繁体   English   中英

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

[英]C++ templating for vector math generating GLSL

我正在尝试提出一种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着色器。

我想出了一种方法。

为所有数据类型定义一个支持GLSL等操作的结构。 请注意,它继承自模板参数B (代表“后端”)。 另请注意,默认情况下,所有功能都是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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM