用于生成GLSL的矢量数学的C ++模板

时间:2017-07-08 13:30:31

标签: c++ templates

我试图想出一个C ++模板设计模式,这个模式允许我编写C ++代码,如下例所示,它能够生成操作的字符串表示(如GLSL),并计算实际结果。

template<typename T>
vec3<T> make_effect(const vec3<T> &color, const mat3<T> &fx_matrix) {
    return sin(fx_matrix * color);
}

// I should of course define a sin function using this style of templating.

(这个例子中的数学没有多大意义,但它给出了我之后的一些暗示。)

请注意,模板参数T。问题是我希望能够使用这个单个代码实例,生成GLSL代码,并根据模板参数在CPU上执行实际计算。

例如,在GLSL作为目标的情况下:

vec3<GLSL> expression = make_effect(vec3<GLSL>(1.0f, 0.4f, 0.1f), some_matrix);
std::string glsl_code = expression.to_string();
// would be:
assert(glsl_code == "sin(some_matrix * vec3(1.0, 0.4, 0.1))");

或者,在CPU端计算的情况下:

vec3<CPU> result = make_effect(vec3<CPU>(1.0f, 0.4f, 0.1f), some_matrix);
// now: result.x(), result.y(), result.z() contain the 
// actual values of the calculation

我应该怎样处理这样的事情?这有名字吗?类似的解决方案是否已经存在?

另请注意,在GLSL变体的情况下,我不想计算任何内容。我只需要代码,就能够为GPU生成着色器。 新实现的可扩展性,例如Direct X着色器是一个优势。

1 个答案:

答案 0 :(得分:0)

我想出了办法。

对于所有数据类型,定义支持GLSL等操作的结构。请注意,它继承自模板参数B(代表&#34;后端&#34;)。另请注意,默认情况下,所有功能均为delete d。下面的示例是float的接口(缩写为fl以避免C ++关键字冲突):

template <typename B>
struct fl : public B {
    fl(B b) : B(b) {}
    fl(float x) = delete;

    operator float() const = delete;

    fl<B> operator+(const fl<B> &rhs) const = delete;
    fl<B> operator*(const fl<B> &rhs) const = delete;
    fl<B> operator-(const fl<B> &rhs) const = delete;
    fl<B> operator/(const fl<B> &rhs) const = delete;

    fl<B> operator-() const = delete;

    vec2<B> operator*(const vec2<B> &rhs) const = delete;
    vec3<B> operator*(const vec3<B> &rhs) const = delete;
};

现在我们将为GLSL定义一个后端用作B

class GLSL {
    std::string expr;

  public:
    operator std::string() const { return expr; }
    operator const char *() const { return expr.c_str(); }

    const std::string &str() const { return expr; }

    GLSL(const std::string &str) : expr(str) {}
    GLSL(const char *str) : expr(str) {}
};

然后,我们将实现GLSL后端模板专业化。例如,GLSL中的fl + fl操作:

template <>
inline fl<GLSL> fl<GLSL>::operator +(const fl<GLSL> &rhs) const {
    return fl<GLSL>("(" + str() + "+" + rhs.str() + ")");
}

此外还有更多实现。

现在我们为CPU后端做一些类似的事情(使用Eigen):

class CPU {
  public:
    union {
        float _fl;
        Eigen::Vector2f _vec2;
        Eigen::Vector3f _vec3;
        struct {
            float _x, _y, _z;
        };
    };

    CPU(float x) { _fl = x; }
    CPU(Eigen::Vector2f x) { _vec2 = x; }
    CPU(Eigen::Vector3f x) { _vec3 = x; }

    CPU(const CPU &orig) { _vec3 = orig._vec3; }
};

fl + fl实施:

template <>
inline fl<CPU> fl<CPU>::operator +(const fl<CPU> &rhs) const {
    return fl<CPU>(_fl + rhs._fl);
}

这适用于所有可能的运算符(二进制和一元),函数等...请注意,我使用了大量的C预处理器宏来节省大量的打字工作。对于矢量类型的调配也完全使用xxx()等函数实现。

现在作为演示:

using namespace vecmath;

template <typename B>
vec3<B> make_effect(vec3<B> a, vec3<B> b) {
    return pow(a, b).yzx() + sin(a);
}

TEST(vecmath_demo, demo_01) {
    // GLSL
    vec3<GLSL> g_a("a");
    vec3<GLSL> g_b("b");
    vec3<GLSL> expr = make_effect(g_a, g_b);

    std::cout << "GLSL: " << expr.str() << std::endl;

    // CPU
    vec3<CPU> c_a(2.0f, 4.0f, 3.0f);
    vec3<CPU> c_b(0.0f, 1.0f, 2.0f);
    vec3<CPU> result = make_effect(c_a, c_b);

    std::cout << "CPU: " << ((Eigen::Vector3f)result).transpose() << std::endl;

    SUCCEED();
}

打印:

GLSL: (pow(a, b).yzx+sin(a))
CPU:  4.9093  8.2432 1.14112