Android open gl2_droidcon_2014

24
Android & OpenGL ES 2+ from an app developers point of view Droidcon Berlin, 2014

Transcript of Android open gl2_droidcon_2014

Page 1: Android open gl2_droidcon_2014

Android & OpenGL ES 2+from an app developers point of view

Droidcon Berlin, 2014

Page 2: Android open gl2_droidcon_2014

Andreas NilssonSoftware specialist @Jayway

!

Broad interest in graphics, both 2D and 3D !

www.jayway.com/blog

Page 3: Android open gl2_droidcon_2014

Inspiration

AndreasEarthBACKUP PLAN(ET)

Page 4: Android open gl2_droidcon_2014

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.

Page 5: Android open gl2_droidcon_2014

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

Page 6: Android open gl2_droidcon_2014

Better control of when things are rendered - Possible to make specific optimizations

Why OpenGL?Control

Page 7: Android open gl2_droidcon_2014

Control

Rendering of ambiguous 3d meshes

Models

Why OpenGL?

Page 8: Android open gl2_droidcon_2014

Control Models Effects

Why OpenGL?

Custom effects, that can only be achieved with a shader

Page 9: Android open gl2_droidcon_2014

Why not RenderScript ?

It is platform specific which limits its usage, etc.My preference: OpenGL + OpenCL

Page 10: Android open gl2_droidcon_2014

Android Wrapping of OpenGL

Model

float[…]

FloatBuffer

Drawing

OpenGL

GLES20

Abstract

Android

Native

Context

GLSurfaceView

EGL

Page 11: Android open gl2_droidcon_2014

EGL

• Platform independent abstract API for setting up an OpenGL context/surface

• Query graphics card for context capabilities

• GLSurfaceView wraps EGL

Page 12: Android open gl2_droidcon_2014

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⇎

Page 13: Android open gl2_droidcon_2014

OpenGL in AndroidGLSurfaceView

GLSurfaceView.Renderer

- A view, set-up for rendering with OpenGL - Runs in its own thread - OpenGL context can be lost onPause()

- Where the actual rendering with OpenGL happens

Page 14: Android open gl2_droidcon_2014

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+

Page 15: Android open gl2_droidcon_2014

1. Create an OpenGL Context

2. Initial OpenGL Setup-clear color, cull mode, etc

3. Load Resources -textures, models, shaders, etc

4. Compile and link shaders

5. Upload Resources to GPU

6. Clear Screen

7. DrawDraw Loop

Rendering: Step-By-Step

Page 16: Android open gl2_droidcon_2014

GLSL ESThe OpenGL Shading Language

Page 17: Android open gl2_droidcon_2014

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

Page 18: Android open gl2_droidcon_2014

Rendering Pipeline5.929688 4.125000 0.000000 5.832031 4.494141 0.000000 5.945313 0.000000 6.429688 4.125000 0.000000 5.387188 4.125000 2.747500 5.297100 4.494141

… x.xxxxxx

][

Vertex Shader Fragment Shader

Simplified!

Page 19: Android open gl2_droidcon_2014

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

Page 20: Android open gl2_droidcon_2014

Simple Vertex Shaderattribute vec4 a_position;attribute vec3 a_color;uniform mat4 mvp;varying vec3 color;void main(){ color = a_color gl_Position = mvp * a_position;}

Calculations are done per vertex

Page 21: Android open gl2_droidcon_2014

Simple Fragment Shader

precision highp float; // ES Specificvarying vec3 color;void main(){ float alpha = 1.0; gl_FragColor = vec4(color, alpha);}

Calculations are done per fragment

Page 22: Android open gl2_droidcon_2014

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

Page 23: Android open gl2_droidcon_2014

Sample Project Demo

github.com/andreasnilsson/OGLHelloWorldExample Project:

Page 24: Android open gl2_droidcon_2014

Questions?

plus.google.com/+AndreasNilsson1985

se.linkedin.com/pub/andreas-nilsson/14/685/966

Droidcon attendees noticeI was unable to host the requested shaders for the earth demo. I might post a blogpost about it in the near future so follow me on google plus or the Jayway blog. You are feel to ask me questions about it though.

Finally, Thanks for your questions and interest in my presentation. Feel free to share any feedback to help me improve my presentations.