【问题标题】:Is the argout paradigm good practice in RenderScript?argout 范式是 RenderScript 中的良好实践吗?
【发布时间】: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


【解决方案1】:

是的,更喜欢将传入分配用于输出的方法的原因是内存重用。创建分配是昂贵的,不应该做超过必要的事情。

第二种方法也会导致“平铺”问题,在这种情况下,您执行多个内核启动以每次填充输出分配的一部分。因为每次以前的内容丢失(或必须复制)时,输出都会重新分配。

【讨论】:

    猜你喜欢
    • 2012-04-08
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2019-01-18
    • 2011-03-09
    • 2015-06-09
    相关资源
    最近更新 更多