gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:20 25 4
gpt4 key购买 nike

我正在尝试想出一个 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(代表“后端”)。另请注意,默认情况下所有函数都是 deleted。下面的示例是 float 的接口(interface)(缩写为 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;
};

现在我们将定义一个后端用作 B,用于 GLSL:

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 预处理器宏来节省大量的输入工作。 vector 类型的调配也完全使用这些函数实现,例如 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

关于用于 vector 数学生成 GLSL 的 C++ 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com