Getting Started with Renderscript
The big reason I got the Xoom the other day was so that I could try out Renderscript. I finally got some time to sit down and mess around with it. It’s pretty nice. Here’s my first app, a port of the RGBCube Processing sketch I posted a long time back.
Renderscript has some really nice features. You write your UI in regular old Android-style Java. Then you write you compute-heavy code in Renderscript (as a .rs file). When your app starts, this gets compiled down to native code for the CPU. Finally, you write shaders for the GPU as .glsl files. Usually this sort of thing gets to be a real headache because of the glue layers which connect the different chunks of code together. But Renderscript comes with some hooks into Eclipse which generate the glue for you. As you add new fields to your .rs file, it generates a Java class with set methods.
The biggest problem with Renderscript is the documentation. It’s a bit sparse. Some of the important things I’ve learned so far are:
- Add try/catches around your initialization. Use Android.util.Log in the catch blocks to report any problems. Otherwise you won’t know about things like typos in your shaders.
- Sometimes when you delete things, the glue classes get out of synch. When that happens, just run “Clean selected project(s)” and rebuild.
- Don’t forget to call setDepth on your RenderScriptGL.SurfaceConfig. None of your DepthFunc settings are going to matter if you didn’t allocate a depth buffer!
- Don’t even bother trying to use the emulator. Just go straight to debugging on the device.
It turns out the part I’ve enjoyed the most was having all of this compute power hooked up to all of the different sensors in the device. That RGBCube demo has the orientation of the cube connected to the tablet’s gyroscope. The cube stays put as you move the tablet around. It does drift a bit. The fix for that is going to be implementing some of the things which David Sachs talks about in this Google Tech Talk.
I’ll try to get this example cleaned up and posted. Having more examples available on the web would be a good thing.