2012-10-29 98 views
1

我正在使用渲染腳本。我想傳遞一個數組的元素來渲染腳本,並且想要在渲染腳本中執行每個元素的平方並將數據返回到android框架的工作。rs和android之間的數據傳輸?

我想通過下面的代碼來做到這一點。

1.java代碼 2.RS代碼

但是通過這些代碼,這東西不是possible.willüPLZ告訴我什麼[R錯誤,我在做 這些代碼。

============================================== ==============================

Java代碼

public class SUM extends Activity { 


private int[] input; 
private int[] output; 
private RenderScript mRS; 
private Allocation mInAllocation; 
private Allocation mOutAllocation; 
private ScriptC_Sum mScript; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    input= new int[4]; 
    input[0]=0; 
    input[1]=1; 
    input[2]=2; 
    input[3]=3; 

    output =new int[4]; 

    createScript(); 
} 


private void createScript() { 

    mRS = RenderScript.create(this); 

    mInAllocation = Allocation.createSized(mRS, Element.U32(mRS),4); 
    mOutAllocation = Allocation.createTyped(mRS,mInAllocation.getType()); 
    mScript = new ScriptC_Sum(mRS, getResources(), R.raw.sum); 

    mScript.bind_v_in(mInAllocation); 
    mScript.bind_v_out(mOutAllocation); 

    mScript.invoke_square(mInAllocation,mOutAllocation); 


} 
} 

======= ================================================== == RS碼

#pragma version(1) 
#pragma rs java_package_name(com.cdacb.mars.summation) 

#include "rs_types.rsh" 
#include "rs_graphics.rsh" 
#include "rs_core.rsh" 

int32_t *v_in ; 
int32_t *v_out; 

void square(){ 

} 

void root(int32_t *v_in,int32_t *v_out) 
{ 

    square(); 

} 

回答

1

有兩件事情:

1-我發現你的.rs文件不能和你的.java文件具有相同的名字

2-你在你的根函數(* v_out)上聲明瞭一個輸出變量,但從來沒有在函數內部計算它。

3-您的Java數組都是整數。從上Renderscript API冪函數的聲明,他們都至少需要一個浮動的輸入

這裏是我的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; 
    } 

} 

這裏是我的Square.rs文件:

#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; 

} 
+0

如果你想繼續使用int而不是float,你可以只做「return * v_in * * v_in;」。在你的代碼中,你也想切換到2.f而不是2,因爲這意味着double-> float轉換(C99語言的一個更糟糕的方面)。 –