1) OpenGL provides low-level control over graphics rendering which allows for customization and optimization compared to higher-level APIs.
2) The OpenGL rendering process in Android involves setting up an OpenGL context, initial setup, loading resources, compiling/linking shaders, uploading resources to the GPU, clearing the screen, and drawing.
3) Shaders written in the OpenGL Shading Language (GLSL) control how vertices and fragments are processed in the rendering pipeline.
4. Terminology
• Matrix - A NxN array of values
• Vertex - A point in space which act as the basic
building block for shapes e.g. line, triangle, etc.
• Mesh - A structure compromised of triangles
• Shader - Small program running on the GPU in
parallell.
5. Background - OpenGL
• An abstract graphics API - That is it
• Written in C but not limited to it
• Bindings for most programming languages
• Low level ⬌ Close to hardware
6. Better control of when things are rendered - Possible to
make specific optimizations
Why OpenGL?
Control
9. Why not RenderScript ?
It is platform specific which limits its usage, etc.
My preference: OpenGL + OpenCL
10. Android Wrapping of OpenGL
Model
float[…]
FloatBuffer
Drawing
OpenGL
GLES20
Abstract
Android
Native
Context
GLSurfaceView
EGL
11. EGL
• Platform independent abstract API for setting up an
OpenGL context/surface
• Query graphics card for context capabilities
• GLSurfaceView wraps EGL
12. OpenGL ES Versions
OpenGL ES 1.x - Fixed pipeline
OpenGL ES 3.x- Extensions and Optimizations
OpenGL ES 2.x - Shader support
OpenGL
OpenGL ES
⇎
⇕
14. GLSurfaceView.Renderer - Quick Look
• onSurfaceCreated() - The OpenGL context is now created
and we can start loading our resources
• onSurfaceChanged() - Called when the size has changed e.g.
device rotation
• onDrawFrame() - Where the fun happens!
• Never mind the parameter GL10, for GLES20+
17. GLSL - OpenGL Shading Language
• A C-style language for writing OpenGL shaders
• OpenGL ES 2.0 has two types of shaders
- Vertex shader
- Fragment shader
• Vertex shader - A program that runs per vertex
• Fragment shader - A program that runs per fragment
19. GLSL Qualifiers
• Uniform - A value that is the same for all vertices and/or pixels
• Attribute - A value defined per vertex, only available in a vertex
shader
• Varying - Data shared between shaders down the pipeline and
since they don’t map 1:1 they are also interpolated
21. Simple Fragment Shader
precision highp float; // ES Specific
varying vec3 color;
void main()
{
float alpha = 1.0;
gl_FragColor = vec4(color, alpha);
}
Calculations are done per fragment
22. We have a battery!
Running @60 FPS for no reason drains the battery
Solution: RENDERMODE_WHEN_DIRTY
General Tips & Tricks
• Limited memory bandwidth - Offload memory bus with computations in shader
• If-statements - In shader is bad for parallelism, both branches are usually
evaluated
• Method calls - In draw-loop may impact performance. Use direct access to data
instead. Bad for encapsulation, good for performance
• Memory allocation - In draw loop should be avoided. Allocate everything on
beforehand or on-demand
• Power of 2 textures - Good practice to use. Non-power-of-two textures has some
limitations in OpenGL ES 2.0