Android High performance in GPU using opengles and renderscript
-
Upload
arvind-devaraj -
Category
Technology
-
view
6.468 -
download
3
Transcript of Android High performance in GPU using opengles and renderscript
Limitless Arvind Devaraj
High Performance Graphics and Compute
OpenGLES and RenderScript
Arvind Devaraj
Limitless Arvind Devaraj
slideshare.net/darvind/
Limitless Arvind Devaraj
Agenda
● Introduction : Graphics terms
● Graphics on GPU
● OpenGL Android Graphics
● Graphics Pipeline
● Shaders
● High Performance Compute on GPU
● RenderScript
Limitless Arvind Devaraj
CPU versus GPU
● CPU
– good at executing sequential code
– Handles branches well
● GPU
– Same code, multiple data
– Parallelism (ideal for image rendering)
Limitless Arvind Devaraj
Graphics Terms
● OpenGLES : Graphics API for doing 3D operations on GPU / CPU
● Primitives : lines, point, triangles
● Texture : make the image realistic by adding bitmap
Limitless Arvind Devaraj
Graphics on GPU
Wireframe of modelPolygonated
Limitless Arvind Devaraj
Graphics Pipeline
VertexTransformation
Pixel operationsVertex
Rasterize the triangle to pixels
Limitless Arvind Devaraj
OpenGL Driver
Converts API call to commands – glDraw()
Commands executed in GPU / CPU
Implementation of the Graphics Pipeline
Limitless Arvind Devaraj
OpenGLES Android Graphics
● Graphics Library for 3D
Limitless Arvind Devaraj
Android Graphics Classes
GLSurfaceViewGLSurfaceView.Renderer
➢ View - connects SurfaceView to OpenGLES library
➢ Renderer - responsible for rendering a frame
Limitless Arvind Devaraj
GLSurfaceView
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new SquareRenderer());
Limitless Arvind Devaraj
GLSurfaceView.Renderer
public class SquareRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 unused, EGLConfig config) { } public void onDrawFrame(GL10 unused) {
} public void onSurfaceChanged(GL10 unused, int width, int height) {
}}
When surface is changed (rotated etc)
draw() - Code for drawing the frame ( Square )
Code when surface is created
Limitless Arvind Devaraj
GLSurfaceView.Renderer
● The renderer is responsible for making OpenGL calls to render a frame.
– onDrawFrame() responsible for drawing the current frame
– OnSurfaceChanged() called when surface size changes
– OnSurfaceCreated() called when surface is created
Limitless Arvind Devaraj
draw() {
float coords[] = { .... };
vertex 'buffer' created with the coords
glVertexPointer(buffer) :
glDrawArrays(TRIANGLE_STRIP, ...);}
Drawing a Square
Limitless Arvind Devaraj
draw() {
float squareCoords[] = { .... };
ByteBuffer vbb = ....
squareVB = vbb.asFloatBuffer(); squareVB.put(squareCoords);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);}
Full code:https://github.com/arvind-devaraj/android-opengles/blob/master/3_SquareRenderer/src/com/example/graphics1/Square.java
Drawing a Square
Limitless Arvind Devaraj
draw() {
float squareCoords[] = { .... };
ByteBuffer vbb = ....
squareVB = vbb.asFloatBuffer(); squareVB.put(squareCoords);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);}
Full code:https://github.com/arvind-devaraj/android-opengles/blob/master/3_SquareRenderer/src/com/example/graphics1/Square.java
Drawing a Square
Limitless Arvind Devaraj
OpenGL Rendering Pipeline
Limitless Arvind Devaraj
Shaders
● Shaders are programs that execute on the GPU
● Shader programs operate on
– Each vertex
– Each pixel
Limitless Arvind Devaraj
Shader Programs
• Vertex Shader – operates on each vertex
• Fragment Shader – operates on each pixel
• Shaders are compiled and linked ( like any program )
• Executable is sent to the GPU
Limitless Arvind Devaraj
Limitless Arvind Devaraj
Vertex Shader
Fragment Shader
attribute vec4 vertexPosition;void main(){ gl_Position = vertexPosition * 2;
}
precision mediump float;void main(){ gl_FragColor = ( color.r + color.g + color.b ) / 3;}
Limitless Arvind Devaraj
Program running on GPU/CPU
Limitless Arvind Devaraj
CPU – GPU communication
Limitless Arvind Devaraj
GPU for General Programs
● Graphics is accelerated by the GPU
● Can GPU accelerate other programs ?
– e.g. Matrix multiply, encryption
● OpenCL, CUDA, Renderscript are API
– used for general purpose GPU
Limitless Arvind Devaraj
RenderScript
● A high level API to access GPU● Provides high performance Compute● Provides uniform API across multiple SoC● Alternative is NDK (but NDK is platform
specific)
Limitless Arvind Devaraj
RenderScript – Use cases
● What functions can be accelerated
– Graphics
– Image Processing
– Encryption
– Signal processing
– Mathematical functions
Limitless Arvind Devaraj
RenderScript - Flow
Limitless Arvind Devaraj
RenderScript – CPU side
Class Hello extends Activity {
Allocation input, output;
Create a script (kernel) that specifies the function
Run script for each element in allocation }
Allocation is how you get data to RS, so that it is processed by kernel
Limitless Arvind Devaraj
RenderScript – CPU side
Class Hello extends Activity {
Allocation input; Allocation output;
RenderScript rs = new RenderScript() ScriptC_func script = new ScriptC_func(...)
script.forEach_root(input, output) }
Allocation is how you get data to RS, so that it is processed by kernel
Limitless Arvind Devaraj
Renderscript Java interaction
Limitless Arvind Devaraj
RenderScript – GPU side
func.rs
void root (char *in , char *out) { *out = *in * 2
}
Renderscript code is compiled to device independent Bitcode
Bitcode is dynamically interpreted by runtime for specifi GPU
Limitless Arvind Devaraj
RenderScript
● Advantages : Compared to NDK, provides an easy device agnostic way to accelerate performance on GPU
● Disadvantages : C99 standard, debugging is restricted
Limitless Arvind Devaraj
Renderscipt - Summary
● Renderscript is an API to access GPU
● Used for High Performance
● High Performance Compute / Graphics
● Compute, Math , FFT, convolution, Signal processing
● Support Graphics – but not a replacement for OpenGL
● Works on all GPUs ( if supported by SoC ) otherwise on CPU
Limitless Arvind Devaraj
Renderscript potential
Limitless
arvinddevaraj
github, linkedin,twitter