Android High performance in GPU using opengles and renderscript

34
Limitless Arvind Devaraj High Performance Graphics and Compute OpenGLES and RenderScript Arvind Devaraj

Transcript of Android High performance in GPU using opengles and renderscript

Page 1: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

High Performance Graphics and Compute

OpenGLES and RenderScript

Arvind Devaraj

Page 2: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

slideshare.net/darvind/

Page 3: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Agenda

● Introduction : Graphics terms

● Graphics on GPU

● OpenGL Android Graphics

● Graphics Pipeline

● Shaders

● High Performance Compute on GPU

● RenderScript

Page 4: Android High performance in GPU using opengles and 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)

Page 5: Android High performance in GPU using opengles and renderscript

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

Page 6: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Graphics on GPU

Wireframe of modelPolygonated

Page 7: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Graphics Pipeline

VertexTransformation

Pixel operationsVertex

Rasterize the triangle to pixels

Page 8: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

OpenGL Driver

Converts API call to commands – glDraw()

Commands executed in GPU / CPU

Implementation of the Graphics Pipeline

Page 9: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

OpenGLES Android Graphics

● Graphics Library for 3D

Page 10: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Android Graphics Classes

GLSurfaceViewGLSurfaceView.Renderer

➢ View - connects SurfaceView to OpenGLES library

➢ Renderer - responsible for rendering a frame

Page 11: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

GLSurfaceView    

    GLSurfaceView view = new GLSurfaceView(this);

    view.setRenderer(new SquareRenderer());

Page 12: Android High performance in GPU using opengles and renderscript

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

Page 13: Android High performance in GPU using opengles and renderscript

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

Page 14: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

draw() {

  float coords[] = { ....     };

vertex 'buffer' created with the coords

glVertexPointer(buffer) :

glDrawArrays(TRIANGLE_STRIP, ...);}

Drawing a Square

Page 15: Android High performance in GPU using opengles and renderscript

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

Page 16: Android High performance in GPU using opengles and renderscript

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

Page 17: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

OpenGL Rendering Pipeline

Page 18: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Shaders

● Shaders are programs that execute on the GPU

● Shader programs operate on

– Each vertex

– Each pixel

Page 19: Android High performance in GPU using opengles and renderscript

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

Page 20: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Page 21: Android High performance in GPU using opengles and renderscript

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

Page 22: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Program running on GPU/CPU

Page 23: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

CPU – GPU communication

Page 24: Android High performance in GPU using opengles and renderscript

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

Page 25: Android High performance in GPU using opengles and renderscript

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)

Page 26: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

RenderScript – Use cases

● What functions can be accelerated

– Graphics

– Image Processing

– Encryption

– Signal processing

– Mathematical functions

Page 27: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

RenderScript - Flow

Page 28: Android High performance in GPU using opengles and renderscript

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

Page 29: Android High performance in GPU using opengles and renderscript

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

Page 30: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Renderscript Java interaction

Page 31: Android High performance in GPU using opengles and renderscript

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

Page 32: Android High performance in GPU using opengles and renderscript

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

Page 33: Android High performance in GPU using opengles and renderscript

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

Page 34: Android High performance in GPU using opengles and renderscript

Limitless Arvind Devaraj

Renderscript potential

Limitless

[email protected]

arvinddevaraj

github, linkedin,twitter