- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试想出一个 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
的接口(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/
我在为 MacOSX 构建的独立包中添加 DMG 背景的自定义图标时遇到问题。我在项目的根目录中添加了一个包。正在从中加载自定义图标,但没有加载 DMG 背景图标。我正在使用 Java fx 2.2.
Qt for Symbian 和 Qt for MeeGo 有什么区别?我知道 Qt 是一个交叉编译平台。这是否意味着如果我使用来自 Qt 的库,完全相同的库可以在所有支持 Qt 的设备(例如 Sym
我正在尝试使用 C# .NET 3.5/4.0 务实地运行 SQL Server 数据库的备份。我已经找到了如何完成此操作,但是我似乎找不到用于备份的命名空间库。 我正在寻找 Microsoft.Sq
我最近在疯狂学习 Java,但我通常是一名 .NET 开发人员。 (所以请原谅我的新手问题。) 在 .Net 中,我可以在不使用 IIS 的情况下开发 ASP.Net 页面,因为它有一个简化的 Web
这post仅当打印命令中有字符串时才有用。现在我有大量的源代码,其中包含一条声明,例如 print milk,butter 应该格式化为 print(milk,butter) 用\n 捕获行尾并不成功
所以我的问题是: https://gist.github.com/panSarin/4a221a0923927115584a 当我保存这个表格时,我收到了标题中的错误 NoMethodError (u
如何让 Html5 音频在点击时播放声音? (ogg 用于 Firefox 等浏览器,mp3 用于 chrome 等浏览器) 到目前为止,我可以通过 onclick 更改为单个文件类型,但我无法像在普
如果it1和it2有什么区别? std::set s; auto it1 = std::inserter(s, s.begin()); auto it2 = std::inserter(s, s.en
4.0.0 com.amkit myapp SpringMVCFirst
我目前使用 Eclipse 作为其他语言的 IDE,而且我习惯于不必离开 IDE 做任何事情 - 但是我真的很难为纯 ECMAScript-262 找到相同或类似的设置。 澄清一下,我不是在寻找 DO
我想将带有字符串数组的C# 结构发送到C++ 函数,该函数接受void * 作为c# 结构和char** 作为c# 结构字符串数组成员。 我能够将结构发送到 c++ 函数,但问题是,无法从 c++ 函
我正在使用动态创建的链接: 我想为f:param附加自定义转换器,以从#{name}等中删除空格。 但是f:param中没有转换器
是否可以利用Redis为.NET创建后写或直写式缓存?理想情况下,透明的高速缓存是由单个进程写入的,并且支持从数据库加载丢失的数据,并每隔一段时间持久保存脏块? 我已经搜查了好几个小时,也许是goog
我正在通过bash执行命令的ssh脚本。 FILENAMES=( "export_production_20200604.tgz" "export_production_log_2020060
我需要一个正则表达式来出现 0 到 7 个字母或 0 到 7 个数字。 例如:匹配:1234、asdbs 不匹配:123456789、absbsafsfsf、asf12 我尝试了([a-zA-Z]{0
我有一个用于会计期间的表格,该表格具有期间结束和开始的开始日期和结束日期。我使用此表来确定何时发生服务交易以及何时在查询中收集收入,例如... SELECT p.PeriodID, p.FiscalY
我很难为只接受字符或数字的 Laravel 构建正则表达式验证。它是这样的: 你好<-好的 123 <- 好的 你好123 <-不行 我现在的正则表达式是这样的:[A-Za-z]|[0-9]。 reg
您实际上会在 Repeater 上使用 OnItemDataBound 做什么? 最佳答案 “此事件为您提供在客户端显示数据项之前访问数据项的最后机会。引发此事件后,数据项将被清空,不再可用。” ~
我有一个 fragment 工作正常的项目,我正在使用 jeremyfeinstein 的 actionbarsherlock 和滑动菜单, 一切正常,但是当我想自定义左侧抽屉列表单元格时,出现异常
最近几天,我似乎平均分配时间在构建我的第一个应用程序和在这里发布问题!! 这是我的第一个应用程序,也是我们的设计师完成的第一个应用程序。我试图满足他所做的事情的外观和感觉,但我认为他没有做适当的事情。
我是一名优秀的程序员,十分优秀!