OpenCL、GCC 和 Clang 都有方便的向量类型扩展 http://clang.llvm.org/docs/LanguageExtensions.html#vectors-and-extended-vectors.
我最喜欢的功能之一是能够进行如下的 swizzle:
float4 a(1,2,3,4);
float4 b = a.xxyw; //1124
我怎样才能制作自己的矢量扩展来做到这一点,例如MSVC 也这样吗?我想出的最好的办法就是float4 b = a.xxyw()
(见下面的代码)。所以我的主要问题是如何在没有()
符号。
如果有人感兴趣,我想出了一些使用定义创建所有排列的代码
#define DEF_SWIZ4(a,b,c,d) Vec4 a##b##c##d() const { return Vec4(a, b, c, d); }
#define DEF_SWIZ3(a,b,c) DEF_SWIZ4(a,b,c,x) DEF_SWIZ4(a,b,c,y) DEF_SWIZ4(a,b,c,z) DEF_SWIZ4(a,b,c,w)
#define DEF_SWIZ2(a,b) DEF_SWIZ3(a,b,x) DEF_SWIZ3(a,b,y) DEF_SWIZ3(a,b,z) DEF_SWIZ3(a,b,w)
#define DEF_SWIZ1(a) DEF_SWIZ2(a,x) DEF_SWIZ2(a,y) DEF_SWIZ2(a,z) DEF_SWIZ2(a,w)
#define DEF_SWIZ() DEF_SWIZ1(x) DEF_SWIZ1(y) DEF_SWIZ1(z) DEF_SWIZ1(w)
class Vec4
{
public:
double x, y, z, w;
Vec4() : x(0), y(0), z(0), w(0) {}
Vec4(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {}
DEF_SWIZ()
};
#include <iostream>
int main()
{
Vec4 v(1, 2, 3, 4);
Vec4 s = v.yyxw();
std::cout << s.x << " " << s.y << " " << s.z << " " << s.w << std::endl;
}