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. 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
8. OpenGL Driver
Converts API call to commands
–
glDraw()
Commands executed in GPU / CPU
Implementation of the Graphics Pipeline
Limitless
Arvind Devaraj
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. 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. Drawing a Square
draw() {
float coords[] = { .... };
vertex 'buffer' created with the coords
glVertexPointer(buffer) :
}
glDrawArrays(TRIANGLE_STRIP ...);
,
Limitless
Arvind Devaraj
18. Shaders
●
Shaders are programs that execute on the GPU
●
Shader programs operate on
–
Each vertex
–
Each pixel
Limitless
Arvind Devaraj
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
23. CPU – GPU communication
Limitless
Arvind Devaraj
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. 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. RenderScript – Use cases
●
What functions can be accelerated
–
Graphics
–
Image Processing
–
Encryption
–
Signal processing
–
Mathematical functions
Limitless
Arvind Devaraj
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. 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
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. 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. 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