我是 Renderscript 的新手,我正在尝试使用 C 结构来表示一些数据。从我正在阅读的内容来看,编译器应该创建一个反射类调用 ScriptField_structName 来让我访问结构内部数据。
这是我的 .rs 文件:
`
#pragma version(1) //first line
#pragma rs java_package_name(com.example.androidmathoptimizationtest) //java package
#pragma rs_fp_imprecise //relax math, will gain access to NEON optimization and others
//this is our first rs file on our own :)
#include "creditCard.rsh"
typedef struct WorldState {
float time;
int galaxyRadius;
float angle;
int audioData[1024];
} WorldState_t;
CreditCard_t *mainList[];
int right;
double red;
static float Compute(int n, float x[]){
float sum = 0;
for(int i =0; i<n; i++){
sum += x[i];
}
return x[1];
}
void root( const float *v_in, float *v_out){
float x[] ={1,2,3} ;
rsGetAllocation(mainList);
*v_out= Compute(sizeof(x)/sizeof(float),x);
}
`
这是我的java声明:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=new TextView(getApplicationContext());
t1 =(TextView) findViewById(R.id.T1);
createCreditCards();
test3();
}
@SuppressLint("NewApi")
public void test3(){
float[] a = new float[5];
for(int i = 0; i<a.length; i++){
a[i]=i;
}
mRS = RenderScript.create(this);
Type t = new Type.Builder(mRS, Element.U8_4(mRS)).create();
Allocation mInAllocation = Allocation.createSized(mRS, Element.I32(mRS), clist.size(), Allocation.USAGE_SCRIPT);
Allocation mOutAllocation = Allocation.createSized(mRS, Element.I32(mRS), clist.size(), Allocation.USAGE_SCRIPT);
ScriptC_test mScript = new ScriptC_test(mRS, getResources(), R.raw.test);
float[] x = new float[a.length];
mInAllocation.copyFrom(a);
mInAllocation.copyTo(x);
mScript.forEach_root(mInAllocation, mOutAllocation);
float[] y =new float[a.length];
mOutAllocation.copyTo(y);
t1.setText(String.format("A array: %s\n\nmInAllocation: %s\n\nmOutAllocation: %s", ArrayToString(a), ArrayToString(x), ArrayToString(y)));
}