How work properly with array allocations in RenderScript

旧时模样 提交于 2019-12-11 16:12:11

问题


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 ...


回答1:


The issue is that rs_allocation is an opaque handle and not a primitive or custom structure the reflected set_*() methods understand. Change your RS code to make coords an int32_t * and have a second type for the length:

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

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

Then in your Java code, you create the allocation but now have to set the coordLen using the reflection method set_coordsLen() (so you can properly bounds check in your RS code, not shown here) then you have to bind the Java side array to the RS allocation:

... 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_coordsLen(coords.length); script.bind_coords(coordsAlloc);

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




回答2:


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.



来源:https://stackoverflow.com/questions/24037029/how-work-properly-with-array-allocations-in-renderscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!