【发布时间】:2013-11-14 00:29:34
【问题描述】:
RenderScript 中的反射类包含执行内核的函数。这些函数遵循 out 参数范式——它们的参数之一是存储输出的分配。
这是否比返回输出分配更好? (我应该效仿并在与 RenderScript 相关的函数中使用参数吗?)
例如,我实现了以下帮助类,它包装了ScriptC_gradient 并计算位图的梯度。它可以从输入 Allocation 推断输出 Allocation 应该具有的类型,从而隐藏设置目标 Allocation 所需的样板。是否有理由更喜欢 compute() 的一种实现而不是另一种?
public class Gradient {
private RenderScript mRS;
private ScriptC_gradient mScript;
public Gradient(RenderScript RS) {
mRS = RS;
mScript = new ScriptC_gradient(mRS);
}
/* Out-argument implementation
*
* This closely mirrors RenderScript's kernel functions, but
* it requires the caller to write boilerplate to set up the
* destination Allocation.
*/
public void compute(Allocation elevation, Allocation gradient) {
mScript.invoke_setDimensions(elevation);
mScript.forEach_root(elevation, gradient);
}
/* Allocation-returning implementation
*
* This hides the boilerplate.
*/
public Allocation compute(Allocation elevation) {
Allocation gradient = Allocation.createTyped(mRS,
new Type.Builder(mRS,Element.F32_2(mRS))
.setX(elevation.getType().getX())
.setY(elevation.getType().getY())
.create(),
Allocation.USAGE_SCRIPT);
mScript.invoke_setDimensions(elevation);
mScript.forEach_root(elevation, gradient);
return gradient;
}
【问题讨论】:
-
我不确定我是否理解。 yourScript.forEach_youKernel() 无法为输出返回分配,因为它无法知道将用于写入输出的分配,除非它作为参数传递。
标签: renderscript