c++ - 如何使用浮点参数生成卤化物函数
问题描述
我正在尝试创建一个 (C++) 辅助函数,它将在我的 C++ 代码中计算的查找表转换为 Halide Func,该函数将浮点数作为参数并在 LUT 中的样本之间进行 lerps。
这里的用例是用户使用带有一堆控制点的样条线生成了色调曲线,我们希望将这些控制点应用于 Halide。所以我在一堆点上采样这个样条,我想写一个卤化物函数,让我在这些样本之间进行线性插值。这是我目前的尝试:
#include <Halide.h>
using namespace Halide;
using namespace std;
static Func LutFunc(const vector<float> &lut) {
Func result;
Var val;
// Copy the LUT into a Halide Buffer.
int lutSize = (int) lut.size();
Buffer<float> lutbuf(lutSize);
for(int i = 0; i < lut.size(); i++) {
lutbuf(i) = lut[i];
}
// Compute the offset into the LUT along with the blending factor
// for the 2 samples we'll take.
auto y = val * ((float) lutSize - 1);
auto index = clamp(cast<int>(y), 0, lutSize - 2);
auto fract = y - cast<float>(index);
// interpolate between the 2 nearest samples
result(val) = (lutbuf(index) * (1.0f - fract)) + (lutbuf(index + 1) * fract);
return result;
}
问题是,如果我尝试将此函数包含到我的 Halide 管道中,我会收到此错误:
Implicit cast from float32 to int in argument 1 in call to "f" is not allowed. Use an explicit cast.
我如何向 Halide 解释这个函数的参数应该是一个浮点数,而不是一个整数?
如果有帮助,这里有一个简短的测试程序:
int main(int argc, char *argv[]) {
vector<float> lut = { 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 };
auto f = LutFunc(lut);
Buffer<float> testData(4);
testData(0) = 0.05;
testData(1) = 0.15;
testData(2) = 0.25;
testData(3) = 0.35;
Func testFn;
Var x;
testFn(x) = f(testData(x));
Buffer<float> resultBuf(4);
testFn.realize(resultBuf);
for(int i = 0; i < 4; i++) {
cout << i << " = " << resultBuf(i) << endl;
}
return 0;
}
(如果有更简单的方法来生成这些 lerping LUT 函数(尤其是如果它能够利用 GPU 上的采样器硬件),我也很想知道这一点)
解决方案
正如您已经发现的那样,参数Halide::Func
始终是整数类型(默认为 int32);这是卤化物固有的。
回复:做 lerping 的更好方法,Halide 有一个内置的lerp()
助手:见http://halide-lang.org/docs/namespace_halide.html#a55158f5f229510194c425dfae256d530
推荐阅读
- java - 使用 mockito 填充列表
- symfony - 带有 Open ID 的 Steam Auth (Symfony 4.x)
- java - PubSubInboundChannelAdapter 在第 4 条消息后停止接收消息
- r - 查找两列的值是否错误切换
- php - MySQL错误我看不懂,grademax错误
- symfony4 - 如何在发送之前 gzip 压缩 Symfony4 http 响应?
- sql - sql select查询求和结果
- java - Android SDK 28 中的 Json 转换出错
- java - 如何获取虚拟机网卡的 IP?
- sql-server - 从根元素xml获取值到列