Fixed swizzle functions and operators tests

This commit is contained in:
Christophe Riccio 2018-07-14 19:56:13 +02:00
parent a23dd2f800
commit 61e68f2667
9 changed files with 129 additions and 106 deletions

View File

@ -187,48 +187,48 @@ namespace detail
//
// Wrapper for a operand between a swizzle and a binary (e.g. 1.0f - u.xyz)
//
#define GLM_SWIZZLE_SCALAR_BINARY_OPERATOR_IMPLEMENTATION(OPERAND) \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER vec<N, T, Q> operator OPERAND ( const GLM_SWIZZLE_TYPE1& a, const T& b) \
{ \
return a() OPERAND b; \
} \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER vec<N, T, Q> operator OPERAND ( const T& a, const GLM_SWIZZLE_TYPE1& b) \
{ \
return a OPERAND b(); \
#define GLM_SWIZZLE_SCALAR_BINARY_OPERATOR_IMPLEMENTATION(OPERAND) \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER vec<N, T, Q> operator OPERAND ( const GLM_SWIZZLE_TYPE1& a, const T& b) \
{ \
return a() OPERAND b; \
} \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER vec<N, T, Q> operator OPERAND ( const T& a, const GLM_SWIZZLE_TYPE1& b) \
{ \
return a OPERAND b(); \
}
//
// Macro for wrapping a function taking one argument (e.g. abs())
//
#define GLM_SWIZZLE_FUNCTION_1_ARGS(RETURN_TYPE,FUNCTION) \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER typename GLM_SWIZZLE_TYPE1::RETURN_TYPE FUNCTION(const GLM_SWIZZLE_TYPE1& a) \
{ \
return FUNCTION(a()); \
#define GLM_SWIZZLE_FUNCTION_1_ARGS(RETURN_TYPE,FUNCTION) \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER typename GLM_SWIZZLE_TYPE1::RETURN_TYPE FUNCTION(const GLM_SWIZZLE_TYPE1& a) \
{ \
return FUNCTION(a()); \
}
//
// Macro for wrapping a function taking two vector arguments (e.g. dot()).
//
#define GLM_SWIZZLE_FUNCTION_2_ARGS(RETURN_TYPE,FUNCTION) \
GLM_SWIZZLE_TEMPLATE2 \
#define GLM_SWIZZLE_FUNCTION_2_ARGS(RETURN_TYPE,FUNCTION) \
GLM_SWIZZLE_TEMPLATE2 \
GLM_FUNC_QUALIFIER typename GLM_SWIZZLE_TYPE1::RETURN_TYPE FUNCTION(const GLM_SWIZZLE_TYPE1& a, const GLM_SWIZZLE_TYPE2& b) \
{ \
return FUNCTION(a(), b()); \
} \
GLM_SWIZZLE_TEMPLATE1 \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER typename GLM_SWIZZLE_TYPE1::RETURN_TYPE FUNCTION(const GLM_SWIZZLE_TYPE1& a, const GLM_SWIZZLE_TYPE1& b) \
{ \
return FUNCTION(a(), b()); \
} \
GLM_SWIZZLE_TEMPLATE1 \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER typename GLM_SWIZZLE_TYPE1::RETURN_TYPE FUNCTION(const GLM_SWIZZLE_TYPE1& a, const typename V& b) \
{ \
return FUNCTION(a(), b); \
} \
GLM_SWIZZLE_TEMPLATE1 \
GLM_SWIZZLE_TEMPLATE1 \
GLM_FUNC_QUALIFIER typename GLM_SWIZZLE_TYPE1::RETURN_TYPE FUNCTION(const V& a, const GLM_SWIZZLE_TYPE1& b) \
{ \
return FUNCTION(a, b()); \

View File

@ -6,7 +6,7 @@
namespace glm{
namespace detail
{
# if GLM_SWIZZLE == GLM_ENABLE
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
template<qualifier Q, int E0, int E1, int E2, int E3>
struct _swizzle_base1<4, float, Q, E0,E1,E2,E3, true> : public _swizzle_base0<float, 4>
{
@ -49,7 +49,7 @@ namespace detail
return Result;
}
};
# endif// GLM_SWIZZLE == GLM_ENABLE
# endif// GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
template<qualifier Q>
struct compute_vec4_add<float, Q, true>

View File

@ -65,10 +65,11 @@ namespace glm
};
# else
union {T x, r, s;};
/*
# if GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
GLM_SWIZZLE_GEN_VEC_FROM_VEC1(T, Q)
# endif
*/
# endif
// -- Component accesses --

View File

@ -50,7 +50,7 @@ int test_vec4_ctor()
}
#endif
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec4 A = glm::vec4(1.0f, 2.0f, 3.0f, 4.0f);
glm::vec4 B = A.xyzw;
@ -79,7 +79,7 @@ int test_vec4_ctor()
Error += glm::all(glm::equal(A, L)) ? 0 : 1;
Error += glm::all(glm::equal(A, M)) ? 0 : 1;
}
# elif GLM_SWIZZLE == GLM_ENABLE
# elif GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec4 A = glm::vec4(1.0f, 2.0f, 3.0f, 4.0f);
glm::vec4 B = A.xyzw();
@ -108,7 +108,7 @@ int test_vec4_ctor()
Error += glm::all(glm::equal(A, L)) ? 0 : 1;
Error += glm::all(glm::equal(A, M)) ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_ENABLE
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec4 A(1);
@ -340,7 +340,7 @@ int test_vec4_swizzle_partial()
glm::vec4 A(1, 2, 3, 4);
# if GLM_HAS_ANONYMOUS_STRUCT && GLM_SWIZZLE == GLM_ENABLE
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec4 B(A.xy, A.zw);
Error += A == B ? 0 : 1;

View File

@ -6,7 +6,7 @@ static int test_ivec2_swizzle()
{
int Error = 0;
# if GLM_SWIZZLE
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
{
glm::ivec2 A(1, 2);
glm::ivec2 B = A.yx();
@ -47,7 +47,7 @@ int test_ivec3_swizzle()
{
int Error = 0;
# if GLM_SWIZZLE == GLM_ENABLE
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
{
glm::ivec3 A(1, 2, 3);
glm::ivec3 B = A.zyx();
@ -56,7 +56,7 @@ int test_ivec3_swizzle()
Error += A != B ? 0 : 1;
Error += A == C ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_ENABLE
# endif
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
@ -94,8 +94,16 @@ int test_ivec3_swizzle()
glm::ivec3 const I = A.xxx / A.xxx;
Error += I == glm::ivec3(1) ? 0 : 1;
glm::ivec3 J(1, 2, 3);
J.xyz += glm::ivec3(1);
Error += J == glm::ivec3(2, 3, 4) ? 0 : 1;
glm::ivec3 K(1, 2, 3);
K.xyz += A.xyz;
Error += K == glm::ivec3(2, 4, 6) ? 0 : 1;
}
# endif//GLM_SWIZZLE
# endif
return Error;
}
@ -104,14 +112,16 @@ int test_ivec4_swizzle()
{
int Error = 0;
#if GLM_SWIZZLE == GLM_ENABLE
glm::ivec4 A(1, 2, 3, 4);
glm::ivec4 B = A.wzyx();
glm::ivec4 C = B.wzyx();
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
{
glm::ivec4 A(1, 2, 3, 4);
glm::ivec4 B = A.wzyx();
glm::ivec4 C = B.wzyx();
Error += A != B ? 0 : 1;
Error += A == C ? 0 : 1;
#endif//GLM_SWIZZLE == GLM_ENABLE
Error += A != B ? 0 : 1;
Error += A == C ? 0 : 1;
}
# endif
return Error;
}
@ -120,17 +130,19 @@ int test_vec4_swizzle()
{
int Error = 0;
#if GLM_SWIZZLE == GLM_ENABLE
glm::vec4 A(1, 2, 3, 4);
glm::vec4 B = A.wzyx();
glm::vec4 C = B.wzyx();
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
{
glm::vec4 A(1, 2, 3, 4);
glm::vec4 B = A.wzyx();
glm::vec4 C = B.wzyx();
Error += A != B ? 0 : 1;
Error += A == C ? 0 : 1;
Error += A != B ? 0 : 1;
Error += A == C ? 0 : 1;
float f = glm::dot(C.wzyx(), C.xyzw());
Error += glm::abs(f - 20.f) < 0.01f ? 0 : 1;
#endif//GLM_SWIZZLE == GLM_ENABLE
float f = glm::dot(C.wzyx(), C.xyzw());
Error += glm::abs(f - 20.f) < 0.01f ? 0 : 1;
}
# endif
return Error;
}

View File

@ -129,7 +129,7 @@ static int test_swizzle()
{
int Error = 0;
#if GLM_HAS_ANONYMOUS_STRUCT && GLM_SWIZZLE == GLM_ENABLE
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec1 A = glm::vec1(1.0f);
//glm::vec1 B = A.x;
@ -138,7 +138,7 @@ static int test_swizzle()
//Error += glm::all(glm::equal(A, B)) ? 0 : 1;
Error += glm::all(glm::equal(A, C)) ? 0 : 1;
}
#endif//GLM_SWIZZLE == GLM_ENABLE
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
return Error;
}

View File

@ -348,7 +348,7 @@ static int test_swizzle()
{
int Error = 0;
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec2 A = glm::vec2(1.0f, 2.0f);
glm::vec2 B = A.xy;
@ -359,7 +359,9 @@ static int test_swizzle()
Error += glm::all(glm::equal(A, C)) ? 0 : 1;
Error += glm::all(glm::equal(A, D)) ? 0 : 1;
}
# elif GLM_SWIZZLE == GLM_ENABLE
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
{
glm::vec2 A = glm::vec2(1.0f, 2.0f);
glm::vec2 B = A.xy();
@ -368,7 +370,7 @@ static int test_swizzle()
Error += glm::all(glm::equal(A, B)) ? 0 : 1;
Error += glm::all(glm::equal(A, C)) ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_ENABLE
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
return Error;
}

View File

@ -317,8 +317,8 @@ int test_vec3_swizzle3_2()
{
int Error = 0;
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec3 v(1, 2, 3);
glm::vec2 u;
@ -371,8 +371,8 @@ 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
# endif//GLM_LANG
}
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
return Error;
}
@ -381,9 +381,10 @@ int test_vec3_swizzle3_3()
{
int Error = 0;
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
glm::vec3 v(1, 2, 3);
glm::vec3 u;
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
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;
@ -398,6 +399,7 @@ int test_vec3_swizzle3_3()
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;
}
# endif//GLM_LANG
return Error;
@ -410,7 +412,8 @@ int test_vec3_swizzle_operators()
glm::vec3 u = glm::vec3(1, 2, 3);
glm::vec3 v = glm::vec3(10, 20, 30);
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec3 q;
// Swizzle, swizzle binary operators
@ -434,6 +437,7 @@ int test_vec3_swizzle_operators()
q = (u.xyz * v); Error += (q == (u * v)) ? 0 : 1;
q = (u.xxx * v); Error += (q == u.x * v) ? 0 : 1;
q = (u.xyz / v); Error += (q == (u / v)) ? 0 : 1;
}
# endif//GLM_LANG
// Compile errors
@ -447,41 +451,41 @@ int test_vec3_swizzle_functions()
{
int Error = 0;
# if GLM_SWIZZLE == GLM_ENABLE
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
{
// NOTE: template functions cannot pick up the implicit conversion from
// a swizzle to the unswizzled type, therefore the operator() must be
// used. E.g.:
//
// glm::dot(u.xy, v.xy); <--- Compile error
// glm::dot(u.xy(), v.xy()); <--- Compiles correctly
// NOTE: template functions cannot pick up the implicit conversion from
// a swizzle to the unswizzled type, therefore the operator() must be
// used. E.g.:
//
// glm::dot(u.xy, v.xy); <--- Compile error
// glm::dot(u.xy(), v.xy()); <--- Compiles correctly
float r;
float r;
// vec2
glm::vec2 a(1, 2);
glm::vec2 b(10, 20);
r = glm::dot(a, b); Error += (int(r) == 50) ? 0 : 1;
r = glm::dot(glm::vec2(a.xy()), glm::vec2(b.xy())); Error += (int(r) == 50) ? 0 : 1;
r = glm::dot(glm::vec2(a.xy()), glm::vec2(b.yy())); Error += (int(r) == 60) ? 0 : 1;
// vec2
glm::vec2 a(1, 2);
glm::vec2 b(10, 20);
r = glm::dot(a, b); Error += (int(r) == 50) ? 0 : 1;
r = glm::dot(glm::vec2(a.xy()), glm::vec2(b.xy())); Error += (int(r) == 50) ? 0 : 1;
r = glm::dot(glm::vec2(a.xy()), glm::vec2(b.yy())); Error += (int(r) == 60) ? 0 : 1;
// vec3
glm::vec3 u = glm::vec3(1, 2, 3);
glm::vec3 v = glm::vec3(10, 20, 30);
r = glm::dot(u, v); Error += (int(r) == 140) ? 0 : 1;
r = glm::dot(u.xyz(), v.zyz()); Error += (int(r) == 160) ? 0 : 1;
r = glm::dot(u, v.zyx()); Error += (int(r) == 100) ? 0 : 1;
r = glm::dot(u.xyz(), v); Error += (int(r) == 140) ? 0 : 1;
r = glm::dot(u.xy(), v.xy()); Error += (int(r) == 50) ? 0 : 1;
// vec3
glm::vec3 u = glm::vec3(1, 2, 3);
glm::vec3 v = glm::vec3(10, 20, 30);
r = glm::dot(u, v); Error += (int(r) == 140) ? 0 : 1;
r = glm::dot(u.xyz(), v.zyz()); Error += (int(r) == 160) ? 0 : 1;
r = glm::dot(u, v.zyx()); Error += (int(r) == 100) ? 0 : 1;
r = glm::dot(u.xyz(), v); Error += (int(r) == 140) ? 0 : 1;
r = glm::dot(u.xy(), v.xy()); Error += (int(r) == 50) ? 0 : 1;
// vec4
glm::vec4 s = glm::vec4(1, 2, 3, 4);
glm::vec4 t = glm::vec4(10, 20, 30, 40);
r = glm::dot(s, t); Error += (int(r) == 300) ? 0 : 1;
r = glm::dot(s.xyzw(), t.xyzw()); Error += (int(r) == 300) ? 0 : 1;
r = glm::dot(s.xyz(), t.xyz()); Error += (int(r) == 140) ? 0 : 1;
#endif//GLM_SWIZZLE == GLM_ENABLE
// vec4
glm::vec4 s = glm::vec4(1, 2, 3, 4);
glm::vec4 t = glm::vec4(10, 20, 30, 40);
r = glm::dot(s, t); Error += (int(r) == 300) ? 0 : 1;
r = glm::dot(s.xyzw(), t.xyzw()); Error += (int(r) == 300) ? 0 : 1;
r = glm::dot(s.xyz(), t.xyz()); Error += (int(r) == 140) ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
return Error;
}
@ -490,25 +494,25 @@ int test_vec3_swizzle_partial()
{
int Error = 0;
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
glm::vec3 A(1, 2, 3);
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec3 const A(1, 2, 3);
glm::vec3 B(A.xy, 3.0f);
Error += A == B ? 0 : 1;
}
{
glm::vec3 B(1.0f, A.yz);
glm::vec3 const A(1, 2, 3);
glm::vec3 const B(1.0f, A.yz);
Error += A == B ? 0 : 1;
}
{
glm::vec3 B(A.xyz);
glm::vec3 const A(1, 2, 3);
glm::vec3 const B(A.xyz);
Error += A == B ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
return Error;
}
@ -544,7 +548,7 @@ static int test_swizzle()
{
int Error = 0;
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec3 A = glm::vec3(1.0f, 2.0f, 3.0f);
glm::vec3 B = A.xyz;
@ -563,7 +567,9 @@ static int test_swizzle()
Error += glm::all(glm::equal(A, G)) ? 0 : 1;
Error += glm::all(glm::equal(A, H)) ? 0 : 1;
}
# elif GLM_SWIZZLE == GLM_ENABLE
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
{
glm::vec3 A = glm::vec3(1.0f, 2.0f, 3.0f);
glm::vec3 B = A.xyz();
@ -582,7 +588,7 @@ static int test_swizzle()
Error += glm::all(glm::equal(A, G)) ? 0 : 1;
Error += glm::all(glm::equal(A, H)) ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_ENABLE
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
return Error;
}

View File

@ -485,7 +485,7 @@ static int test_swizzle_partial()
glm::vec4 A(1, 2, 3, 4);
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec4 B(A.xy, A.zw);
Error += A == B ? 0 : 1;
@ -511,7 +511,7 @@ static int test_swizzle_partial()
glm::vec4 B(1.0f, A.yzw);
Error += A == B ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_ENABLE
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
return Error;
}
@ -520,7 +520,7 @@ static int test_swizzle()
{
int Error = 0;
# if GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec4 A = glm::vec4(1.0f, 2.0f, 3.0f, 4.0f);
glm::vec4 B = A.xyzw;
@ -549,7 +549,9 @@ static int test_swizzle()
Error += glm::all(glm::equal(A, L)) ? 0 : 1;
Error += glm::all(glm::equal(A, M)) ? 0 : 1;
}
# elif GLM_SWIZZLE == GLM_ENABLE
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
{
glm::vec4 A = glm::vec4(1.0f, 2.0f, 3.0f, 4.0f);
glm::vec4 B = A.xyzw();
@ -578,7 +580,7 @@ static int test_swizzle()
Error += glm::all(glm::equal(A, L)) ? 0 : 1;
Error += glm::all(glm::equal(A, M)) ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_ENABLE && GLM_HAS_ANONYMOUS_STRUCT
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR || GLM_SWIZZLE == GLM_SWIZZLE_FUNCTION
return Error;
}