【问题标题】:How work properly with array allocations in RenderScript如何在 RenderScript 中正确处理数组分配
【发布时间】:2014-07-25 01:54:16
【问题描述】:

我已经使用 RenderScript 工作了几天,但我不知道如何正确地将数组从 Java 传递到 RenderScript。我看到了一些示例,但没有一个对我有用,而且我因缺乏文档而陷入困境。

在这段代码中,我试图在 bb 和 coords 数组之间为 root() 接收到的每个索引 in 做一些检查:

渲染脚本代码:

#pragma version(1)
#pragma rs java_package_name(com.me.example)

int4 bb;

rs_allocation coords;

void __attribute__((kernel)) root(int32_t in) 
{
   int index = in;

   if(bb[index] > rsGetElementAt_int(coords, index))
   {
       if(bb[index + 1] > rsGetElementAt_int(coords, index + 1))
       {
             //do something
       }
   }  
}

Java 代码:

RenderScript mRS = RenderScript.create(this);        
ScriptC_script script = new ScriptC_script(mRS, getResources(), R.raw.match); 

// This arrays comes with data from another place
int[] indices; 
int[] coords;

// Create allocations
Allocation mIN = Allocation.createSized(mRS, Element.I32(mRS), indices.length);  
Allocation mOUT = Allocation.createSized(mRS, Element.I32(mRS), indices.length); 
Allocation coordsAlloc = Allocation.createSized(mRS, Element.I32(mRS), coords.length);    

// Fill it with data
mIN.copyFrom(indices);
coordsAlloc.copyFrom(coords);

// Set the data array
script.set_coords(coordsAlloc);

// Create bb and run
script.set_bb(new Int4 (x, y, width, height));
script.forEach_root(mIN);       

当我执行它时,我在 set_coords() 语句中收到此错误:

Script::setVar 无法设置分配,无效槽索引

然后程序退出:

致命信号 11 (SIGSEGV) 位于 0x00000000 (code=1),线程 12274 ...

【问题讨论】:

    标签: android arrays allocation renderscript


    【解决方案1】:

    感谢您的回复拉里。我尝试了您的方法,但在 set_coordsLen() 语句中出现了一个新错误。

    Script::setSlot 无法设置分配,无效的槽索引

    所以我开始认为我的脚本中一定还有另一个问题。我从头开始再次检查所有内容,并在另一堂课上发现了问题。我从另一个 .rs 文件中创建了错误的脚本(复制粘贴失败):

    ScriptC_script exmple = new ScriptC_script(mRS, getResources(), R.raw.exmple);
    ScriptC_script script = new ScriptC_script(mRS, getResources(), R.raw.exmple);
    

    代替:

    ScriptC_script exmple = new ScriptC_script(mRS, getResources(), R.raw.exmple);
    ScriptC_script script = new ScriptC_script(mRS, getResources(), R.raw.script); 
    

    我收到这些错误是因为我在不存在的变量上设置分配。这个小(也是可耻的)错误让我沮丧了太多小时。 Eclipse 让我调用设置方法仍然很奇怪。

    我尝试了两种传递数组的方法,这次都成功了。不过我更喜欢你的。

    P.D:我看了你的 AnDevCon 演示视频。再次感谢您分享您对 RS 的了解。

    【讨论】:

    • ScriptC_script script = new ScriptC_script(mRS);应该也可以。我们将构造函数更改为在最新的编译器中使用单个参数。
    • 感谢您的建议。效果很好!
    【解决方案2】:

    问题在于 rs_allocation 是一个不透明的句柄,而不是反射 set_*() 方法理解的原始或自定义结构。更改您的 RS 代码以使坐标成为 int32_t * 并为长度设置第二种类型:

    int32_t *coords; int32_t coordsLen; ... void attribute((kernel)) root(int32_t in) { int index = in;

    if(bb[index] > 坐标[index]) { if(bb[index + 1] > 坐标[index + 1]) { //做一点事 } }
    }

    然后在您的 Java 代码中,您创建分配,但现在必须使用反射方法 set_coordsLen() 设置 coordLen(这样您就可以正确地检查您的 RS 代码,此处未显示)然后您必须 将 Java 端数组绑定到 RS 分配:

    ... Allocation coordsAlloc = Allocation.createSized(mRS, Element.I32(mRS), coords.length);

    // 用数据填充它 minIN.copyFrom(索引); coordsAlloc.copyFrom(coords);

    // 设置数据数组 script.set_coordsLen(coords.length); script.bind_coords(coordsAlloc);

    // 创建bb并运行 script.set_bb(new Int4 (x, y, width, height)); script.forEach_root(mIN);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多