rs和android之间的数据传输?
我正在使用渲染脚本。我想传递数组元素以渲染脚本和 要对渲染脚本中的每个元素执行平方运算,并将数据返回到 android框架 我正试图通过以下代码来实现这一点 1.java代码 2.RS代码 但是通过这些代码,这是不可能的。请告诉我我在做什么 用这些代码 ============================================================================ java代码 =========================================================== RS码 有几件事: 1-我发现.rs文件不能与.java文件同名 2-您在根函数(*v_out)上声明了一个输出变量,但从未在函数内部计算它 3-您的java数组都是int。从上的幂函数声明中,它们都至少接受一个浮点作为输入 以下是我的java代码:rs和android之间的数据传输?,android,renderscript,Android,Renderscript,我正在使用渲染脚本。我想传递数组元素以渲染脚本和 要对渲染脚本中的每个元素执行平方运算,并将数据返回到 android框架 我正试图通过以下代码来实现这一点 1.java代码 2.RS代码 但是通过这些代码,这是不可能的。请告诉我我在做什么 用这些代码 ============================================================================ java代码 =======================================
public class Sum extends Activity {
private float[] input;
private float[] output;
private RenderScript mRS;
private Allocation mInAllocation;
private Allocation mOutAllocation;
private TextView t1;
private ScriptC_Square mScript;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sum);
t1= new TextView(this);
t1= (TextView)findViewById(R.id.textview1);
input= new float[4];
input[0]=0;
input[1]=1;
input[2]=2;
input[3]=3;
output =new float[input.length];
createScript();
}
private void createScript() {
mRS = RenderScript.create(this);
mInAllocation = Allocation.createSized(mRS, Element.F32(mRS),4);
mOutAllocation = Allocation.createTyped(mRS,mInAllocation.getType());
mScript = new ScriptC_Square(mRS, getResources(), R.raw.square);
mInAllocation.copyFrom(input);
mScript.forEach_root(mInAllocation, mOutAllocation); // calls the forEach function to operate on the root function (each allocation input, will have a corresponding allocation output
mOutAllocation.copyTo(output); // copy the result that was stored in mOutAllocation into the array output
t1.setText(String.format("Input:%s\n\noutput:%s",
ArrayToString(input), ArrayToString(output)));
}
/**
* this function just print each element of a primitive float array into a text string
* @param array
* @return
*/
public String ArrayToString(float[] array){
String s="";
for(int i=0; i<array.length; i++){
s+= String.format("\nValue %d: %f", i, array[i]);
}
return s;
}
}
有几件事:
1-我发现.rs文件不能与.java文件同名
2-您在根函数(*v_out)上声明了一个输出变量,但从未在函数内部计算它
3-您的java数组都是int。从上的幂函数声明中,它们都至少接受一个浮点作为输入
以下是我的java代码:
public class Sum extends Activity {
private float[] input;
private float[] output;
private RenderScript mRS;
private Allocation mInAllocation;
private Allocation mOutAllocation;
private TextView t1;
private ScriptC_Square mScript;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sum);
t1= new TextView(this);
t1= (TextView)findViewById(R.id.textview1);
input= new float[4];
input[0]=0;
input[1]=1;
input[2]=2;
input[3]=3;
output =new float[input.length];
createScript();
}
private void createScript() {
mRS = RenderScript.create(this);
mInAllocation = Allocation.createSized(mRS, Element.F32(mRS),4);
mOutAllocation = Allocation.createTyped(mRS,mInAllocation.getType());
mScript = new ScriptC_Square(mRS, getResources(), R.raw.square);
mInAllocation.copyFrom(input);
mScript.forEach_root(mInAllocation, mOutAllocation); // calls the forEach function to operate on the root function (each allocation input, will have a corresponding allocation output
mOutAllocation.copyTo(output); // copy the result that was stored in mOutAllocation into the array output
t1.setText(String.format("Input:%s\n\noutput:%s",
ArrayToString(input), ArrayToString(output)));
}
/**
* this function just print each element of a primitive float array into a text string
* @param array
* @return
*/
public String ArrayToString(float[] array){
String s="";
for(int i=0; i<array.length; i++){
s+= String.format("\nValue %d: %f", i, array[i]);
}
return s;
}
}
如果想继续使用int而不是float,只需执行“return*v_-in**v_-in;”。在上面的代码中,您还需要切换到2.f而不是2,因为这意味着一个double->float转换(C99语言最糟糕的方面之一)。如果您想继续使用int而不是float,您可以执行“return*v_In**v_In;”。在上面的代码中,您还希望切换到2.f而不是2,因为这意味着双->浮点转换(C99语言最糟糕的方面之一)。
public class Sum extends Activity {
private float[] input;
private float[] output;
private RenderScript mRS;
private Allocation mInAllocation;
private Allocation mOutAllocation;
private TextView t1;
private ScriptC_Square mScript;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sum);
t1= new TextView(this);
t1= (TextView)findViewById(R.id.textview1);
input= new float[4];
input[0]=0;
input[1]=1;
input[2]=2;
input[3]=3;
output =new float[input.length];
createScript();
}
private void createScript() {
mRS = RenderScript.create(this);
mInAllocation = Allocation.createSized(mRS, Element.F32(mRS),4);
mOutAllocation = Allocation.createTyped(mRS,mInAllocation.getType());
mScript = new ScriptC_Square(mRS, getResources(), R.raw.square);
mInAllocation.copyFrom(input);
mScript.forEach_root(mInAllocation, mOutAllocation); // calls the forEach function to operate on the root function (each allocation input, will have a corresponding allocation output
mOutAllocation.copyTo(output); // copy the result that was stored in mOutAllocation into the array output
t1.setText(String.format("Input:%s\n\noutput:%s",
ArrayToString(input), ArrayToString(output)));
}
/**
* this function just print each element of a primitive float array into a text string
* @param array
* @return
*/
public String ArrayToString(float[] array){
String s="";
for(int i=0; i<array.length; i++){
s+= String.format("\nValue %d: %f", i, array[i]);
}
return s;
}
}
#pragma version(1)
#pragma rs java_package_name(com.example.sum)//don't forget to change that package name
void root(const float *v_in, float *v_out ){
*v_out = pow(*v_in, 2); //use the internal pow function of Renderscript pow(float x, float y) = x ^y;
}