This Android OpenGL ES 3.0 Sample was written by Cristiano Ferreira - Software Engineer at Intel Corporation.
By batch-rendering multiple versions of the same mesh, instanced rendering can help developers using OpenGL* ES 3.0 and later dramatically improve performance and resource efficiency. The sample code introduced here demonstrates a simple technique for using this capability.
Instanced rendering allows for the reuse of geometry information, to render multiple instances of the same mesh with a single API call. Developers can use uniforms to apply unique attributes to specific instances, which provides for variation using visual properties such as texture. By reducing overhead—and therefore resource requirements—to render large numbers of elements, instanced rendering offers the potential to improve visual experiences, especially on hardware-constrained mobile devices.
Instancing was first introduced in OpenGL 3.1 as an arb extension (ARB_draw_instanced). This capability has now been added to the mobile space, with its inclusion as a core feature in OpenGL ES 3.0.
Downloading and installing the sample
The code sample is available for download from GitHub. To build and deploy the application to your Android device, choose either of the approaches described below.
Approach 1: Use BUILDANDROID.BAT
- Navigate to InstancingAndroid\Utilities
- Run buildandroid.bat
Approach 2: Build from the command line
- Navigate to InstancingAndroid\InstancingAndroid
- Run the following:
android update project –p .
Details of the implementation
This sample implements instancing by means of a uniform buffer that contains transform matrices for the model instances. The implementation operates as follows:
- The first step is filling the buffer with our transform data.
- When rendering, we simply call glDrawElementsInstanced instead of glDrawElements and pass in the number of instances as a parameter.
- Submitting the geometry information only once, glDrawElementsInstanced uses those same resources repeatedly to draw the desired number of instances.
- Each instance is assigned a unique identifier, as a value of gl_InstanceID that ranges from 0 up to ((num instances)-1). That value is used as an index to the uniform buffer to extract the current instances transform matrix and other properties.
While instancing does not require significant added work on the developer’s part, it can significantly increase rendering efficiency and performance.
Performance comparison: instanced versus non-instanced rendering
Instanced rendering is traditionally used for very large numbers of instances, on relatively simple (low poly) meshes. As shown in this sample, however, this capability can also be used effectively for fairly complex structures. The following chart compares the performance using an instanced draw call versus the non-instanced approach with multiple draw calls. As the number of instances increases, instanced rendering demonstrates a clear performance benefit, measured in terms of higher frame rates.
The instancing technique shown in this sample gives developers a simple means of substantially increasing performance in their OpenGL ES 3.0 applications. Instanced rendering lowers the resource requirements to provide rich, dynamic backgrounds, allowing developers to replace more limited billboarding techniques. That helps bring visual experiences that approach desktop-quality 3D graphics on mobile devices.
OpenGL ES: The Standard for Embedded Accelerated 3D Graphics is Khronos Group’s official OpenGL ES page; it describes the standard and provides links to specifications, header files, and reference materials.
Android Developer Guide for OpenGL ES provides background and guidance for use of the OpenGL ES API with Android, including management of compatibility between the two.
OpenGL® ES 3.0 and Beyond: How To Deliver Desktop Graphics on Mobile Platforms describes capabilities of both OpenGL ES 3.0 and 3.1, including how they are supported on Intel’s Bay Trail platform.
Intel® Open Source Technology Center Intel Graphics for Linux* provides downloads, documentation, and community resources related to graphics for Linux on Intel® platforms.