如何在RenderScript阵列分配正常工作 [英] How work properly with array allocations in RenderScript
问题描述
我一直在RenderScript周围工作了几天,但我不知道如何正确地从Java数组传递到renderScript。我看到一些例子,但他们没有工作对我来说,我不让自己被缺少文档。
在此code,我试图做BB之间有一些检查 COORDS 数组,每个索引在的根()接收:
RenderScript code:
的#pragma版(1)
RS的#pragma java_package_name(com.me.example)INT4 BB;rs_allocation COORDS;无效__attribute __((内核))根(int32_t中)
{
INT指数=中; 如果(BB [指数]> rsGetElementAt_int(COORDS,指数))
{
如果(BB [索引+ 1] GT; rsGetElementAt_int(COORDS,指数+ 1))
{
//做一点事
}
}
}
的Java code:
RenderScript的mRS = RenderScript.create(本);
ScriptC_script脚本=新ScriptC_script(MRS,getResources(),R.raw.match);//此阵列自带来自另一个地方的数据
INT []指标;
INT [] COORDS;//创建分配
分配分钟= Allocation.createSized(MRS,Element.I32(MRS),indices.length);
分配MOUT = Allocation.createSized(MRS,Element.I32(MRS),indices.length);
分配coordsAlloc = Allocation.createSized(MRS,Element.I32(MRS),coords.length);//用数据填充
mIN.copyFrom(指数);
coordsAlloc.copyFrom(COORDS);//设置数据数组
script.set_coords(coordsAlloc);//创建BB和运行
script.set_bb(新INT4(X,Y,宽度,高度));
script.forEach_root(MIN);
当我执行它,我得到这个错误上的 set_coords()的语句:
脚本:: SETVAR无法设置分配,插槽无效指数
块引用>和程序退出:
在00000000(code = 1),螺纹12274致命信号11(SIGSEGV)...
块引用>解决方案感谢您的答复拉里。我想你的方法,我在得到了一个新的错误的 set_coordsLen()的声明。
脚本:: setSlot无法设置分配,插槽无效指数
块引用>所以,我开始觉得我必须在我的脚本另一个问题。我从一开始就再次检查一切,我发现这个问题的另一个类。我创建我的脚本错误,从另一个.RS文件(copypaste失败):
ScriptC_script exmple =新ScriptC_script(MRS,getResources(),R.raw.exmple);
ScriptC_script脚本=新ScriptC_script(MRS,getResources(),R.raw.exmple);而不是:
ScriptC_script exmple =新ScriptC_script(MRS,getResources(),R.raw.exmple);
ScriptC_script脚本=新ScriptC_script(MRS,getResources(),R.raw.script);我得到这些错误,因为我是在设置变量不存在分配。这一点(和可耻的)错误花了我的挫折太多时间。尽管如此奇怪了Eclipse让我调用该方法集
我试图传递数组和的两种方式都工作这个时候。我虽然preFER你。
Pd积:我看了你的AnDevCon presentation视频。双感谢分享RS的知识。
I've been working around with RenderScript for a few days, but I can't figure out how properly pass an array from Java to RenderScript. I saw some examples but none of them worked for me and I'm getting stuck with the lack of documentation.
In this code I'm trying to do some checks between bb and coords array for each index in that root() receives:
RenderScript code:
#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 code:
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);
When I execute it I get this error on set_coords() statement:
Script::setVar unable to set allocation, invalid slot index
And program exits:
Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 12274 ...
解决方案Thanks for your reply Larry. I tried your approach and I got a new error in set_coordsLen() statement.
Script::setSlot unable to set allocation, invalid slot index
So I starting to think that I must have another problem in my script. I checked everything again from the beginning and I found the problem in another class. I was creating my script wrong, from another .rs file (copypaste fail):
ScriptC_script exmple = new ScriptC_script(mRS, getResources(), R.raw.exmple); ScriptC_script script = new ScriptC_script(mRS, getResources(), R.raw.exmple);
Instead of:
ScriptC_script exmple = new ScriptC_script(mRS, getResources(), R.raw.exmple); ScriptC_script script = new ScriptC_script(mRS, getResources(), R.raw.script);
I was getting those errors because I was setting allocations on inexistent variables. This little (and shameful) mistake cost me too many hours of frustration. Still weird that Eclipse let me invoke that sets methods.
I tried both ways of passing the array and both worked this time. I prefer yours though.
P.D: I watched your AnDevCon presentation video. Double thanks for share your knowledge about RS.
这篇关于如何在RenderScript阵列分配正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!