2014, 13th Kandroid minmax GPU, Graphics and Networking

OpenGL and EGL

SK플래닛/모바일 플랫폼 개발팀 남정수 ([email protected]) http://www.linkedin.com/in/yegam400 목 차

OpenGL • What is OpenGL • OpenGL ES 2.0 Rendering Pipeline • GLSL(OpenGL Shading Language) • Android GLSL Usages

EGL • OpenGL Utility Libraries • EGL Overview • EGL Operations • EGL Usages • Android GLSurfaceView

OpenGL : What is OpenGL

• OpenGL – OpenGL (Open Graphics Library)[2] is a cross-language, multi-platform application programming interface ( API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a Graphics processin g unit (GPU), to achieve hardware-accelerated rendering. – OpenGL was developed by Inc. (SGI) from 1991 and released in January 1992[3] and is wid ely used in CAD, virtual reality, scientific visualization, information visualization, flight simulation, and video g ames. OpenGL is managed by the non-profit technology consortium Khronos Group. • OpenGL ES – OpenGL for Embedded Systems (OpenGL ES or GLES) is a subset of the OpenGL – It is designed for embedded systems like smartphones, computer tablets, video game consoles and PDAs. • OpenGL development – In addition to the features required by the core API, GPU vendors may provide additional functionality in the f orm of extensions. Extensions may introduce new functions and new constants, and may relax or remove re strictions on existing OpenGL functions. Vendors can use extensions to expose custom without needin g support from other vendors or the Khronos Group as a whole, which greatly increases the flexibility of Ope nGL. All extensions are collected in, and defined by, the OpenGL Registry.

2014, 13th Kandroid minmax - www.kandroid.org 3 OpenGL : OpenGL ES 2.0 Rendering Pipeline

• Fixed Function Pipeline removed at ES2.0

2014, 13th Kandroid minmax - www.kandroid.org 4 OpenGL : Shading and Shaders

• Shading – Shading refers to depicting depth perception in 3D models or illustrations by varying levels of darkness. – In computer graphics, shading refers to the process of altering the color of an object/surface/ polygon in the 3D scene, based on its angle to lights and its distance from lights to create a p hotorealistic effect. Shading is performed during the rendering process by a program called a shader.

2014, 13th Kandroid minmax - www.kandroid.org 5 OpenGL : Transform and Lighting

emissive = Ke

ambient = Ka x globalAmbient

diffuse = Kd x lightColor x max(N · L, 0)

specular = Ks x lightColor x facing x (max(N · H, 0)) s hininess surfaceColor = emissive + ambient + diffuse + specular

2014, 13th Kandroid minmax - www.kandroid.org 6 OpenGL : Rasterization & Raster Operations(ROP)

2014, 13th Kandroid minmax - www.kandroid.org 7 OpenGL : What is GLSL

• GLSL – OpenGL Shading Language (abbreviated: GLSL or GLslang), is a high-level shading langu age based on the syntax of the C programming language. It was created by the OpenGL AR B (OpenGL Architecture Review Board) to give developers more direct control of the graphic s pipeline without having to use ARB assembly language or hardware-specific languages. – Supports OpenGL ES(Android, iOS, and etc.)

2014, 13th Kandroid minmax - www.kandroid.org 8 OpenGL : GLSL – Simple Example

ftransform() is used for fixed function pipeline.

2014, 13th Kandroid minmax - www.kandroid.org 9 OpenGL : GLSL keywords – attribute, uniform, varying

• (Vertex) Attribute – Vertex attributes are used to communicate from outside to the vertex shader. • Unlike uniform variables, values are provided per vertex (and not globally for all vertices). • There are built-in vertex attributes like the normal or the position, or you can specify your own vertex at tribute like a tangent or another custom value. • Attributes can't be defined in the fragment shader. • Uniform – Uniform variables are used to communicate with your vertex or fragment shader from "outsid e". In your shader you use the uniform qualifier to declare the variable • Uniform variables are read-only and have the same value among all processed vertices. You can only change them within your C++ program. • Varying – Varying variables provide an interface between Vertex and Fragment Shader. • Vertex Shaders compute values per vertex and fragment shaders compute values per fragment. • If you define a varying variable in a vertex shader, its value will be interpolated (perspective-correct) ov er the primitive being rendered and you can access the interpolated value in the fragment shader.

2014, 13th Kandroid minmax - www.kandroid.org 10 OpenGL : GLSL - Build-in Variables(1/2)

2014, 13th Kandroid minmax - www.kandroid.org 11 OpenGL : GLSL - Build-in Variables(2/2)

2014, 13th Kandroid minmax - www.kandroid.org 12 OpenGL : GLSL APIs

• GLSL flow APIs – Loading Shader • glCreateShaderObject() • glShaderSource() – Compiling Shader • glCompileShader() – Linking • glCreateProgramObject() • glAttachObject() • glLinkProgram() – Using Shaders • glUseProgramObject() • GLSL communication APIs – Attribute • glGetAttribLocation() • glEnableVertexAttribArray() • glVertexAttribPointer() – Uniform • glGetUniformLocation() • glUniform()

2014, 13th Kandroid minmax - www.kandroid.org 13 OpenGL : GLSL Vertex Shader Example – Directional Lighting

2014, 13th Kandroid minmax - www.kandroid.org 14 OpenGL : GLSL Fragment Shader Example – Per Pixel Directional Lighting

http://www.lighthouse3d.com/tutorials/glsl-tutorial/directional-light-per-pixel/

2014, 13th Kandroid minmax - www.kandroid.org 15 OpenGL : GLSL Fragment Shader Example – YUV to RGB Conversion

http://helloworld.naver.com/helloworld/1207

2014, 13th Kandroid minmax - www.kandroid.org 16 OpenGL : GLSL Fragment Shader Example – Image Filter(Box Blur)

2014, 13th Kandroid minmax - www.kandroid.org 17 OpenGL : Android GLSL Usages(1/4) - Initializing

2014, 13th Kandroid minmax - www.kandroid.org 18 OpenGL : Android GLSL Usages(2/4) - Shaders

2014, 13th Kandroid minmax - www.kandroid.org 19 OpenGL : Android GLSL Usages(3/4) – Rendering environment setup

2014, 13th Kandroid minmax - www.kandroid.org 20 OpenGL : Android GLSL Usages(4/4) – Rendering a mesh

2014, 13th Kandroid minmax - www.kandroid.org 21 목 차

OpenGL • What is OpenGL • OpenGL ES 2.0 Rendering Pipeline • GLSL(OpenGL Shading Language) • Android GLSL Usages

EGL • OpenGL Utility Libraries • EGL Overview • EGL Operations • EGL Usages • Android GLSurfaceView

EGL : OpenGL ARB and Khronos Group

• OpenGL ES – OpenGL ES is a lightweight graphics API which is designed for Embedded System from Ope nGL which is designed for Work Station. – OpenGL is maintained by OpenGL ARB(Architecture Review Board), OpenGL ES is maintained Khronos Group.

2014, 13th Kandroid minmax - www.kandroid.org 23 EGL : OpenGL Utility Libraries

Prefix Name Functions gl OpenGL API glClear,glDrawArrays,…

glu OpenGL Utility Library gluPerspective,gluLookAt

glut OpenGL Utility Toolkit glutInitDisplayMode,glutSwapBuffers

aux OpenGL Auxiliary Library auxDIBImageLoad,auxInitWindow

glew OpenGL Extension Wrangler Library glewInit, glewIsSupported

/agl/cgl// Native Interface for OpenGL wglCreateContext,wglMakeCurrent,e glCreateContext,eglMakeCurrent

2014, 13th Kandroid minmax - www.kandroid.org 24 EGL : WGL

• OpenGL Native Interface for Windows – WGL or Wiggle is an API between OpenGL and the interface of . • http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/

ChoosePixelFormat()

SetPixelFormat()

wglCreateContext()

wglMakeCurrent()

2014, 13th Kandroid minmax - www.kandroid.org 25 EGL : EGL Overview

• Native Platform Interface – EGL™ is an interface between Khronos rendering APIs such as OpenGL ES or OpenVG and the underlying native platform window system. It handles graphics context management, surf ace/buffer binding, and rendering synchronization and enables high-performance, accelerate d, mixed-mode 2D and 3D rendering using other Khronos APIs. EGL also provides interop ca pability between Khronos to enable efficient transfer of data between APIs – for example bet ween a video subsystem running OpenMAX AL and a GPU running OpenGL ES. • Portable Layer for Graphics Resource Management – EGL can be implemented on multiple operating systems (such as Android and Linux) and native window systems (such as X and Microsoft Windows). Implementations may also choose to allow rendering into specific types of EGL surfaces via other supported native rendering APIs, such as Xlib or GDI. EGL provides: • Mechanisms for creating rendering surfaces (windows, pbuffers, pixmaps) onto which client APIs can d raw and share • Methods to create and manage graphics contexts for client APIs • Ways to synchronize drawing by client APIs as well as native platform rendering APIs

2014, 13th Kandroid minmax - www.kandroid.org 26 EGL : EGL Features

• Specifically EGL is a wrapper over the following subsystems; – WGL: Windows GL-the Windows-OpenGL interface (pronounced wiggle) – CGL: the Mac OS X-OpenGL interface (the AGL layer sits on top of CGL) – GLX: the equivalent X11-OpenGL interface • EGL not only provides a convenient binding between the resources and the OpenGL subsystem, but also provides the hooks to the operating system to i nform it when you require something, such as; 1. Iterating, selecting, and initializing an OpenGL context. 2. This can be the OGL API level, software vs. hardware rendering, etc. 3. Requesting a surface or memory resource. 4. The OS services requests for system or video memory. 5. Iterating through the available surface formats (to pick an optimal one) 6. You can find out properties of the video card(s) from the OS – the surfaces presented will re sides on the video card(s) or software renderer interface. 7. Selecting the desired surface format. 8. Informing the OS you are done rendering and it’s time to show the scene. 9. Informing the OS to use a different OpenGL context. 10. Informing the OS you are done with the resources.

2014, 13th Kandroid minmax - www.kandroid.org 27 EGL : EGLWindow, EGLDisplay, EGLSurface

• EGLDisplay – Abstract display on which graphics are drawn – Single physical screen – Initialize by querying a default display – All EGL objects are associated with an EGLDisplay • EGLContext(Rendering Contexts) – EGLContext is a state machine defined by a client API – EGLContext is associated with EGLSurfaces • EGLSurface(Drawing Surfaces) – Types: windows, pbuffers, pixmaps • Windows, pixmaps: tied to native window system – Created with EGLConfig • Describes depth of color buffer component and types, quantities, and sizes of the ancillary buffers(depth, multisample, stencil buffers) – Ancillary buffers are associated with an EGLSurface • Not EGLContext

2014, 13th Kandroid minmax - www.kandroid.org 28 EGL : EGL Operation(1/2)

• Interaction with native rendering – Native rendering will always be supported by pixmap surfaces • Pixmap surfaces have restricted capabilities and performance relative to window and pbuffer surfaces – Native rendering will not be supported by pbuffer surfaces, since the color buffers of pbuffers are allocated internally by EGL and are not accessible through any other means. – Native rendering may be supported by window surfaces, but only if the native window system has a compatible rendering model allowing it to share the back color buffer, or if single buffered rendering to the window surface is being done. – When both native rendering APIs and client APIs are drawing into the same underlying surface, no guarantees are placed on the relative order of completion of operations in the different rendering streams other than those provided by the synchronization primitives discussed in section 3.8 • Shared State – OpenGL and OpenGL ES Texture Objects • OpenGL and OpenGL ES makes no attempt to synchronize access to texture objects. If a texture object is bound to more than one context, then it is up to the programmer to ensure that the contents of the object are not being changed via one context while another context is using the texture object for rendering. The results of changing a texture object while another context is using it are undefined. – OpenGL and OpenGL ES Buffer Objects • hen it is up to the programmer to ensure that the contents of the object are not being changed via one context while another context is using the buffer object for rendering. The results of changing a buffer object while another context is using it are undefined. • Multiple Threads – EGL guarantees sequentiality within a command stream for each of its client APIs , but not between these APIs and native APIs which may also be rendering into the same surface. – Client API commands are not guaranteed to be atomic. • Synchronization is in the hands of the client. • It can be maintained at moderate cost with the judicious use of commands such as glFinish, vgFinish, eglWait-Client, and eglWaitNative, as well as (if they exist) synchronization commands present in native rendering APIs.

2014, 13th Kandroid minmax - www.kandroid.org 29 EGL : EGL Operation(2/2)

• Power Management – Power management events can occur synchronously while an application is running. When the system returns from the power management event the EGLContext will be invalidated, and all subsequent client API calls will have no effect (as if no context is bound). – Following a power management event, calls to eglSwapBuffers, eglCopy-Buffers, or eglMakeCurrent will indicate failure by returning EGL_FALSE. The error EGL_CONTEXT_LOST will be returned if a power management event has occurred. • On detection of this error, the application must destroy all contexts (by calling eglDestroyContext for each context). To continue rendering the application must recreate any contexts it requires, and subsequently restore any client API state and objects it wishes to use. • Any EGLSurfaces that the application has created need not be destroyed following a power management event, but their contents will be invalid.

2014, 13th Kandroid minmax - www.kandroid.org 30 EGL : OpenGL Framebuffer

• Framebuffer – A Framebuffer is a collection of buffers that can be used as the destination for rendering.

Functions Parameters glClear GL_COLOR_BUFFER_BIT,GL_DEPTH_BUFFER_BIT,GL_STENCIL_B UFFER_BIT,GL_ACCUM_BUFFER_BIT glEnable GL_DEPTH_TEST, GL_STENCIL_TEST

Name Description

Color Buffer Double buffering, stereo buffering glDrawBuffer,glReadBuffer,glClearColor Depth Buffer Shadow map, internal rendering glDepthFunc,glClearDepth Stencil Buffer Shadow volume, color masking, reflection glStencilFunc,glStencilOp,glClearStencil Accum Buffer Motion blur, anti-aliasing glAccum,glClearAccum

2014, 13th Kandroid minmax - www.kandroid.org 31 EGL : EGL Basic Usage

• The basic usage of EGL and similar API are the following; 1. (Android) Obtain the EGL interface. • So you can make EGL calls 2. Obtain a display that’s associated with an app or physical display 3. Initialize the display 4. Configure the display 5. Create surfaces • Front, back, offscreen buffers, etc. 6. Create a context associated with the display • This holds the “state” for the OpenGL calls 7. Make the context “current” • This selects the active state 8. Render with OpenGL (OpenGL not EGL calls, the OpenGL state is held by EGL context) 9. Flush or swap the buffers so EGL tells the OS to display the rendered scene. Repeat rend ering till done. 10. Make the context “not current” 11. Clean up the EGL resources

2014, 13th Kandroid minmax - www.kandroid.org 32 EGL : EGL API(1/3)

Initializing Funtion Description EGLDisplay eglGetDisplay(EGLNativeDisplayType EGL_DEFAULT_DISPLAY display_id); EGLBoolean eglInitialize(EGLDisplay dpy, EGLint the values of *major and *minor would be 1 and 2, respectively). *major, EGLint *minor); major and minor are not updated if they are specified as NULL. EGLBoolean eglTerminate(EGLDisplay dpy); const char *eglQueryString(EGLDisplay dpy, EGLint EGL_CLIENT_APIS, EGL_EXTENSIONS, EGL_VENDOR, or EGL_ name); VERSION.

Rendering to Textures Funtion Description EGLBoolean eglBindTexImage(EGLDisplay dpy, Bind OpenGL ES Texture EGLSurface surface, EGLint buffer); EGLBoolean eglReleaseTexImage(EGLDisplay dpy, Release OpenGL ES Texture EGLSurface surface, EGLint buffer);

Posting the color buffer Funtion Description

EGLBoolean eglSwapBuffers(EGLDisplay dpy, Posting to a Window EGLSurface surface); When native window resizing, called automatically EGLBoolean eglCopyBuffers(EGLDisplay dpy, Copying to a Native Pixmap EGLSurface surface, EGLNativePixmapType target); EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint Control swap buffer interval interval);

EGL Extension Funtion Description

void (*eglGetProcAddress(const char *procname))(void); eglQueryString(dpy, EGL_EXTENSIONS)

2014, 13th Kandroid minmax - www.kandroid.org 33 EGL : EGL API(2/3)

Configuration Funtion Description

EGLBoolean eglGetConfigs(EGLDisplay dpy, Get configs EGLConfig *configs, EGLint config_size, EGLint *num_config); EGLBoolean eglChooseConfig(EGLDisplay Choose configs by attrib_list dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config); EGLBoolean eglGetConfigAttrib(EGLDisplay Get config attribs dpy, EGLConfig config, EGLint attribute, EGLint *value);

2014, 13th Kandroid minmax - www.kandroid.org 34 EGL : EGL API(3/3)

Surface Funtion Description

EGLSurface eglCreateWindowSurface(EGLDisplay dpy, Create surface for window rendering EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list); EGLSurface eglCreatePbufferSurface(EGLDisplay dpy, Create surface for offline rendering EGLConfig config, const EGLint *attrib_list); EGLSurface eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype, EGLClientBuffer Create surface for offline rendering buffer, EGLConfig config, const EGLint *attrib_list); from client buffer

EGLSurface eglCreatePixmapSurface(EGLDisplay dpy, Create surface for pixmap rendering EGLConfig config, EGLNativePixmapType pixmap, const EGLint *attrib_list); EGLBoolean eglDestroySurface(EGLDisplay dpy, Destroy a surface EGLSurface surface); EGLBoolean eglSurfaceAttrib(EGLDisplay dpy, Get surface attributes EGLSurface surface, EGLint attribute, EGLint value); EGLBoolean eglQuerySurface(EGLDisplay dpy, Get surface attributes EGLSurface surface, EGLint attribute, EGLint *value);

2014, 13th Kandroid minmax - www.kandroid.org 35 EGL : EGL API Usage(1/2)

2014, 13th Kandroid minmax - www.kandroid.org 36 EGL : EGL API Usage(2/2)

2014, 13th Kandroid minmax - www.kandroid.org 37 EGL : GLSurfaceView

• Features – Inherited from SurfaceView – EGL encapsulation • Functions – setRenderer() – setEGLConfigChooser() • Default egl config chooser included – setRenderMode() • RENDERMODE_WHEN_DIRTY • requestRender() – setDebugFlags() • DEBUG_CHECK_GL_ERROR • DEBUG_LOG_GL_CALLS

2014, 13th Kandroid minmax - www.kandroid.org 38 EGL : GLSurfaceView.Renderer

• Functions – onSurfaceCreate() • Create window surface – onSurfaceChanged() • Surface size changed – onDrawFrame() • Automatically eglSwapBuffers when non-dirty rendering

2014, 13th Kandroid minmax - www.kandroid.org 39 EGL : EGL PBuffer

2014, 13th Kandroid minmax - www.kandroid.org 40 2014, 13th Kandroid minmax GPU, Graphics and Networking

Q & A

SK플래닛/모바일 플랫폼 개발팀 남정수 ([email protected]) http://www.linkedin.com/in/yegam400