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
...
CPU versus GPU
●

CPU
–

good at executing sequential code

–

●

Handles branches well

–

Same code, multiple data

GPU
...
Graphics Terms
●

●

●

OpenGLES : Graphics API for doing 3D operations on GPU /
CPU
Primitives : lines, point, triangles
...
Graphics on GPU

Wireframe of model
Polygonated
Limitless

Arvind Devaraj
Graphics Pipeline

Vertex

Pixel
operations

Vertex
Transformation
Rasterize the
triangle to pixels

Limitless

Arvind Dev...
OpenGL Driver

Converts API call to commands
–

glDraw()

Commands executed in GPU / CPU
Implementation of the Graphics Pi...
OpenGLES Android Graphics
●

Graphics Library for 3D

Limitless

Arvind Devaraj
Android Graphics Classes
GLSurfaceView
GLSurfaceView.Renderer

➢

View

- connects SurfaceView to OpenGLES library

➢

Ren...
GLSurfaceView
    

    GLSurfaceView view = new GLSurfaceView(this);

    view.setRenderer(new SquareRenderer());

Limitl...
GLSurfaceView.Renderer
public class SquareRenderer implements GLSurfaceView.Renderer {
    public void onSurfaceCreated(GL...
GLSurfaceView.Renderer

●

The renderer is responsible for making OpenGL calls to
render a frame.
–

onDrawFrame() respons...
Drawing a Square
draw() {
 

float coords[] = { ....     };
vertex 'buffer' created with the coords
glVertexPointer(buffer...
Drawing a Square
draw() {
 

float squareCoords[] = { ....     };
ByteBuffer vbb = ....
  squareVB = vbb.asFloatBuffer(); ...
Drawing a Square
draw() {
 

float squareCoords[] = { ....     };
ByteBuffer vbb = ....
  squareVB = vbb.asFloatBuffer(); ...
OpenGL Rendering Pipeline

Limitless

Arvind Devaraj
Shaders
●

Shaders are programs that execute on the GPU

●

Shader programs operate on
–

Each vertex

–

Each pixel

Limi...
Shader Programs
• Vertex Shader – operates on each vertex
• Fragment Shader – operates on each pixel
• Shaders are compile...
Limitless

Arvind Devaraj
Vertex Shader
attribute vec4 vertexPosition;
void main(){
     gl_Position = vertexPosition * 2;
 

}

Fragment Shader
pre...
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 m...
RenderScript
●

A high level API to access GPU

●

Provides high performance Compute

●

Provides uniform API across multi...
RenderScript – Use cases
●

What functions can be accelerated
–

Graphics

–

Image Processing

–

Encryption

–

Signal p...
RenderScript - Flow

Limitless

Arvind Devaraj
RenderScript – CPU side
Class Hello extends Activity {
Allocation input, output;
Allocation is how you get data to RS, so ...
RenderScript – CPU side
Class Hello extends Activity {
Allocation input;
Allocation output;
RenderScript rs = new RenderSc...
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 devi...
RenderScript
●

●

Advantages : Compared to NDK, provides an easy device
agnostic way to accelerate performance on GPU
Dis...
Renderscipt - Summary
●

Renderscript is an API to access GPU

●

Used for High Performance

●

High Performance Compute /...
Renderscript potential
Limitless
arvind.iisc@gmail.com
arvinddevaraj
github, linkedin,twitter

Limitless

Arvind Devaraj
Upcoming SlideShare
Loading in...5
×

Android High performance in GPU using opengles and renderscript

7,233

Published on

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,233
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
73
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Android High performance in GPU using opengles and renderscript

  1. 1. High Performance Graphics and Compute OpenGLES and RenderScript Arvind Devaraj Limitless Arvind Devaraj
  2. 2. slideshare.net/darvind/ Limitless Arvind Devaraj
  3. 3. Agenda ● Introduction : Graphics terms ● Graphics on GPU ● OpenGL Android Graphics ● Graphics Pipeline ● Shaders ● High Performance Compute on GPU ● RenderScript Limitless Arvind Devaraj
  4. 4. CPU versus GPU ● CPU – good at executing sequential code – ● Handles branches well – Same code, multiple data GPU – Limitless Parallelism (ideal for image rendering) Arvind Devaraj
  5. 5. 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
  6. 6. Graphics on GPU Wireframe of model Polygonated Limitless Arvind Devaraj
  7. 7. Graphics Pipeline Vertex Pixel operations Vertex Transformation Rasterize the triangle to pixels Limitless Arvind Devaraj
  8. 8. OpenGL Driver Converts API call to commands – glDraw() Commands executed in GPU / CPU Implementation of the Graphics Pipeline Limitless Arvind Devaraj
  9. 9. OpenGLES Android Graphics ● Graphics Library for 3D Limitless Arvind Devaraj
  10. 10. Android Graphics Classes GLSurfaceView GLSurfaceView.Renderer ➢ View - connects SurfaceView to OpenGLES library ➢ Renderer - responsible for rendering a frame Limitless Arvind Devaraj
  11. 11. GLSurfaceView          GLSurfaceView view = new GLSurfaceView(this);     view.setRenderer(new SquareRenderer()); Limitless Arvind Devaraj
  12. 12. GLSurfaceView.Renderer public class SquareRenderer implements GLSurfaceView.Renderer {     public void onSurfaceCreated(GL10 unused, EGLConfig config) {             Code when surface is created     }     public void onDrawFrame(GL10 unused) { draw() - Code for drawing the frame ( Square )     }     public void onSurfaceChanged(GL10 unused, int width, int height)   {         When surface is changed (rotated etc)     } } Limitless Arvind Devaraj
  13. 13. 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
  14. 14. Drawing a Square draw() {   float coords[] = { ....     }; vertex 'buffer' created with the coords glVertexPointer(buffer) : } glDrawArrays(TRIANGLE_STRIP ...); , Limitless Arvind Devaraj
  15. 15. Drawing a Square 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/androidopengles/blob/master/3_SquareRenderer/src/com/example/graphics1/Square.java Limitless Arvind Devaraj
  16. 16. Drawing a Square 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/androidopengles/blob/master/3_SquareRenderer/src/com/example/graphics1/Square.java Limitless Arvind Devaraj
  17. 17. OpenGL Rendering Pipeline Limitless Arvind Devaraj
  18. 18. Shaders ● Shaders are programs that execute on the GPU ● Shader programs operate on – Each vertex – Each pixel Limitless Arvind Devaraj
  19. 19. 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
  20. 20. Limitless Arvind Devaraj
  21. 21. Vertex Shader attribute vec4 vertexPosition; void main(){      gl_Position = vertexPosition * 2;   } Fragment Shader precision mediump float; void main(){     gl_FragColor = ( color.r + color.g + color.b ) / 3; } Limitless Arvind Devaraj
  22. 22. Program running on GPU/CPU Limitless Arvind Devaraj
  23. 23. CPU – GPU communication Limitless Arvind Devaraj
  24. 24. 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
  25. 25. 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
  26. 26. RenderScript – Use cases ● What functions can be accelerated – Graphics – Image Processing – Encryption – Signal processing – Mathematical functions Limitless Arvind Devaraj
  27. 27. RenderScript - Flow Limitless Arvind Devaraj
  28. 28. RenderScript – CPU side Class Hello extends Activity { Allocation input, output; Allocation is how you get data to RS, so that it is processed by kernel Create a script (kernel) that specifies the function Run script for each element in allocation } Limitless Arvind Devaraj
  29. 29. 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
  30. 30. Renderscript Java interaction Limitless Arvind Devaraj
  31. 31. 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
  32. 32. 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
  33. 33. 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
  34. 34. Renderscript potential Limitless arvind.iisc@gmail.com arvinddevaraj github, linkedin,twitter Limitless Arvind Devaraj
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×