Monday, 28 May 2012

Program Renderscript on Android

Renderscript (introduced in Android 3.0) offers a high performance 3D graphics rendering and compute API at the native level that you write in C (C99 standard).

The Renderscript runtime operates at the native level and still needs to communicate with the Android VM, so the way a Renderscript application is setup is different from a pure VM application. An application that uses Renderscript is still a traditional Android application that runs in the VM, but you write Renderscript code for the parts of your program that require it. Using Renderscript can be as simple as offloading a few math calculations or as complicated as rendering an entire 3D game. No matter what you use it for, Renderscript remains platform independent, so you do not have to target multiple architectures (for example, ARM v5, ARM v7, x86).

The Renderscript system adopts a control and slave architecture where the low-level Renderscript runtime code is controlled by the higher level Android system that runs in a virtual machine (VM). The Android VM still retains all control of memory management and binds memory that it allocates to the Renderscript runtime, so the Renderscript code can access it. The Android framework makes asynchronous calls to Renderscript, and the calls are placed in a message queue and processed as soon as possible.


It's a very basic example of Program Renderscript on Android. It simple draw a triangle on RSSurfaceView.

Renderscript on Android


First of all, to use Renderscript, we have to define Renderscript code in .rs(Renderscript) and .rsh(Renderscript header) files in the src/ directory of Android project.

RenderScript.rs
The init() method will be called when it's first time loaded. The root() will be called every time the script run. The return parameter of root() define the repeat interval of the script.
//declare RendorScript Version
#pragma version(1)

//declare package
#pragma rs java_package_name(com.exercise.AndroidRenderScript);

//include graphics library
#include "rs_graphics.rsh"

rs_mesh my_rs_mesh;

void init(){

}

int root(){
//set background color
rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);

//draw something
rsgDrawMesh(my_rs_mesh);

//repeat 20ms
return 20;
}

Once you save the rs file, the coresponding files ScriptC_RenderScript.java and /res/raw/renderscript.bc will be generated. The file name depends on your rs file



Create MyRSSurfaceView class extends RSSurfaceView.
The class ScriptC_RenderScript and R.raw.renderscript are generated from the rs file.
package com.exercise.AndroidRenderScript;

import android.content.Context;
import android.renderscript.Mesh;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScriptGL;
import android.renderscript.RenderScriptGL.SurfaceConfig;

public class MyRSSurfaceView extends RSSurfaceView {

private RenderScriptGL renderScriptGL;
private ScriptC_RenderScript myScriptC;

public MyRSSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
final RenderScriptGL.SurfaceConfig surfaceConfig
= new SurfaceConfig();
renderScriptGL = createRenderScriptGL(surfaceConfig);

myScriptC = new ScriptC_RenderScript(
renderScriptGL, getResources(), R.raw.renderscript);

//Create something...
Mesh.TriangleMeshBuilder myMesh
= new Mesh.TriangleMeshBuilder(
renderScriptGL,
2,
Mesh.TriangleMeshBuilder.COLOR);
myMesh.addVertex(0, 0);
myMesh.addVertex(0, 100);
myMesh.addVertex(100, 0);
myMesh.addTriangle(0, 1, 2);
Mesh mesh = myMesh.create(true);
myScriptC.set_my_rs_mesh(mesh);
//

renderScriptGL.bindRootScript(myScriptC);
}

}

Finally, modify the main activity to set MyRSSurfaceView as content view.

package com.exercise.AndroidRenderScript;

import android.app.Activity;
import android.os.Bundle;

public class AndroidRenderScriptActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyRSSurfaceView(this));
}
}


Download the files.


Next:
- Perform transform of Translate and Rotate on RenderScript


No comments:

Post a Comment