8

I'm working on Android with OpenGL. I known how to use GLSurfaceView and its custom derivate classes to create OpenGL ES 2.0 context with method of GLSurfaceView:

setEGLContextClientVersion(2); 

and OpenGL ES 3.0 context:

setEGLContextClientVersion(3); 

How can i create a context for OpenGL ES 3.1?

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
xcesco
  • 4,690
  • 4
  • 34
  • 65
  • This likely can be done by rolling out a custom GLSurfaceView. I know something custom is necessary to render OpenGL ES into a TextureView. I referred to [GLSurfaceView source (not official)](https://github.com/ykulbashian/LiquidSurface/blob/master/liquidview/src/main/java/com/mycardboarddreams/liquidsurface/GLTextureView.java) in order to learn how to create EGL Context and to specify the proper major and minor GLES version numbers. – Nicholas Miller Sep 12 '16 at 06:00

1 Answers1

8

You can't explicitly request 3.1 when you create the context. Based on my understanding, 3.1 is not handled as a context type separate from 3.0. Essentially, a context supporting 3.1 is just a 3.0 context that also supports the additional 3.1 features.

This means that you can still use:

setEGLContextClientVersion(3);

If you want to check/verify what version is supported by the context, you can query it once you have the context up and running:

int[] vers = new int[2];
GLES30.glGetIntegerv(GLES30.GL_MAJOR_VERSION, vers, 0);
GLES30.glGetIntegerv(GLES30.GL_MINOR_VERSION, vers, 1);
if (vers[0] > 3 || (vers[0] == 3 && vers[1] >= 1)) {
    // We have at least ES 3.1.
}

Background

The latest version of EGL, which is 1.5 [*], actually does have context creation attributes that allow specifying both a major and minor version (attributes EGL_CONTEXT_MAJOR_VERSION and EGL_CONTEXT_MINOR_VERSION). Versions up to and including 1.4 only have EGL_CONTEXT_CLIENT_VERSION, so they have no mechanism to specify the minor version when creating a context.

The latest released version of Android, which is 5.1.1 [*], still only supports EGL 1.4. So it's not only a question of GLSurfaceView not providing an interface. The lower native layers do not support specifying a minor version either. So adding 3.1 support to 3.0 contexts is really the only option.

[*] At the time this answer was written.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • Note: GLSurfaceView doesn't support GLES 3 pre-Lollipop (5.0). If you need GLES 3 in KitKat and earlier, you'll need to use a plain SurfaceView and do the EGL setup yourself. (See https://android.googlesource.com/platform/frameworks/base/+/28a5c593577d6aef03d874b42ec0215c88f62bf4%5E%21/opengl/java/android/opengl/GLSurfaceView.java .) – fadden Oct 04 '15 at 16:35
  • @fadden I have used ES 3.0 with GLSurfaceView on KitKat systems. After looking at the code, I'm not completely sure why it actually worked. One possibility is that vendors made the necessary modifications to GLSurfaceView. Also, the context creation directly takes the specified version. It's only the config selection that looks incomplete. So if you override the config chooser, or vendors use the same configs for various versions (which they mostly do), it would all work. – Reto Koradi Oct 04 '15 at 16:52
  • I'll take your word for it -- I saw the "if not 2, then it must be 1" and gave up a while back. I'm disappointed the code continues to silently accept unsupported values. FWIW, the key advantage of 3.1 is for apps in stores like Google Play, where you can specify a baseline feature level. IIRC the various mobile GPU vendors already had most or all of the 3.1 features in their 3.0 drivers, but it's more work for app devs to run-time test for features and provide low-quality fallbacks for unsupported extensions, so they tend to code for the lowest common denominator. – fadden Oct 04 '15 at 17:32
  • Nice find on EGL 1.5, I just realized that EGL 1.4 doesn't actually have this. I found the [EGL 1.5 spec](https://www.khronos.org/registry/egl/specs/eglspec.1.5.pdf) and it specifies on pages 51-52 about these new attributes and how they are used in conjuction with the `eglCreateContext` function. – Nicholas Miller Sep 12 '16 at 06:06