2012-10-29 3 views
1

Je travaille avec le script de rendu. je veux passer des éléments d'un tableau à rendre le script et voulez effectuer la quadrature de chaque élément dans le script de rendu et de récupérer les données à travail de trame Android. J'essaye de faire ceci par les codes suivants.Transfert de données entre RS et Android?

Code 1.java 2.RS Code

Mais par ces codes cette chose ne possible.will u plz me dire quelles sont les erreurs que je fais r avec ces codes.

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

code 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 CODE

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

} 

Répondre

1

Un couple de choses:

1- Je découvre que votre fichier .rs ne peut avoir le même nom que votre fichier .java

2- vous avez déclaré une variable de sortie sur votre fonction racine (* v_out) mais ne la calculez jamais dans la fonction.

3- Vos tableaux java sont tous des entiers. De la déclaration de fonction de puissance sur le Renderscript API, ils prennent tous au moins un flotteur en entrée

Voici mon code 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; 
    } 

} 

Voici mon fichier 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

Si vous voulez continuer à utiliser int au lieu de float, vous pouvez simplement faire "return * v_in * * v_in;". Dans votre code ci-dessus, vous voudriez également passer à 2.f au lieu de simplement 2, car cela impliquerait une conversion double-> float (l'un des pires aspects du langage C99). –

Questions connexes