high performance graphics and computation - opengl es and renderscript

28
High Performance Graphics and Compute OpenGLES and RenderScript Arvind Devaraj

Upload: blrdroid

Post on 12-May-2015

736 views

Category:

Technology


0 download

DESCRIPTION

Arvind Devraj's overview of OpenGL and RenderScript

TRANSCRIPT

Page 1: High performance graphics and computation - OpenGL ES and RenderScript

High Performance Graphics and Compute

OpenGLES and RenderScript

Arvind Devaraj

Page 2: High performance graphics and computation - OpenGL ES and RenderScript

Agenda

● Introduction : Graphics terms

● Graphics on GPU

● OpenGL Android Graphics

● Graphics Pipeline

● Shaders

● High Performance Compute on GPU

● RenderScript

Page 3: High performance graphics and computation - OpenGL ES and RenderScript

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 4: High performance graphics and computation - OpenGL ES and RenderScript

CPU versus GPU

● CPU

– good at executing sequential code

– Handles branches well

● GPU

– Same code, multiple data

– Parallelism (ideal for image rendering)

Page 5: High performance graphics and computation - OpenGL ES and RenderScript

Graphics on GPU

Page 6: High performance graphics and computation - OpenGL ES and RenderScript

Graphics Pipeline

Page 7: High performance graphics and computation - OpenGL ES and RenderScript

OpenGLES Android Graphics

● Graphics Library for 3D

Page 8: High performance graphics and computation - OpenGL ES and RenderScript

Android Graphics Classes

GLSurfaceViewGLSurfaceView.Renderer

➢View - connects SurfaceView to OpenGLES library

➢Renderer - responsible for making OpenGL calls to render a frame

Page 9: High performance graphics and computation - OpenGL ES and RenderScript

GLSurfaceView    

    GLSurfaceView view = new GLSurfaceView(this);

    view.setRenderer(new SquareRenderer());

Page 10: High performance graphics and computation - OpenGL ES and RenderScript

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 11: High performance graphics and computation - OpenGL ES and RenderScript

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)

Code for drawing the frame ( Square )

Code when surface is created

Page 12: High performance graphics and computation - OpenGL ES and RenderScript

draw() {

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

vertex 'buffer' created with the coords

glVertexPointer(buffer) :

glDrawArrays(TRIANGLE_STRIP, ...);}

Drawing a Square

Page 13: High performance graphics and computation - OpenGL ES and RenderScript

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 14: High performance graphics and computation - OpenGL ES and RenderScript

OpenGL Rendering Pipeline

Page 15: High performance graphics and computation - OpenGL ES and RenderScript

Shaders

● Shaders are programs that execute on the GPU

● Shader programs operate on

– Each vertex

– Each pixel

Page 16: High performance graphics and computation - OpenGL ES and RenderScript

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 17: High performance graphics and computation - OpenGL ES and RenderScript
Page 18: High performance graphics and computation - OpenGL ES and RenderScript

Vertex Shader

    

Fragment Shader

   

attribute vec4 vertexPosition;void main(){  gl_Position = vertexPosition;

}

precision mediump float;void main(){ gl_FragColor = vec4(0.5, 0.5, 0.5, 1.0);}

Page 19: High performance graphics and computation - OpenGL ES and RenderScript
Page 20: High performance graphics and computation - OpenGL ES and RenderScript

CPU – GPU communication

Page 21: High performance graphics and computation - OpenGL ES and RenderScript

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 22: High performance graphics and computation - OpenGL ES and RenderScript

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 23: High performance graphics and computation - OpenGL ES and RenderScript

RenderScript – Use cases

● What functions can be accelerated

– Graphics

– Image Processing

– Encryption

– Signal processing

– Mathematical functions

Page 24: High performance graphics and computation - OpenGL ES and RenderScript

RenderScript - Flow

Page 25: High performance graphics and computation - OpenGL ES and RenderScript

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

Page 26: High performance graphics and computation - OpenGL ES and RenderScript

RenderScript – GPU side

Hello.rs

void root (char *in , char *out) { *out = *in * 2

}

Page 27: High performance graphics and computation - OpenGL ES and RenderScript

RenderScript

● Advantages : Compared to NDK, provides an easy device agnostic way to accelerate performance on GPU

● Disadvantages : C99 standard, debugging is restricted

Page 28: High performance graphics and computation - OpenGL ES and RenderScript

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