Java – Share resources between OpenGL contexts on Android

Share resources between OpenGL contexts on Android… here is a solution to the problem.

Share resources between OpenGL contexts on Android

I’m trying to load textures outside of my main thread using multiple EGL contexts. I get an EGL_BAD_CONTEXT error after the eglCreateContext call.

In my android.opengl.Renderer

public void onSurfaceCreated (javax.microedition.khronos.opengles.GL10 gl, EGLConfig config) {
    // ...
    EGLContext sharedContext = egl.getCurrentContext();
    EGLDisplay display = eglGetCurrentDisplay();
    eglCreateContext(display, config, sharedContext, new int[] { EGL_CONTEXT_CLIENT_VERSION, 2 } );
}

EGL_BAD_CONTEXT leads me to the documentation here , that is

EGL_BAD_CONTEXT is generated if share_context is not an EGL rendering context of the same client API type as the newly created context and is not EGL_NO_CONTEXT.

That’s why I added it in the EGL_CONTEXT_CLIENT_VERSION parameter, but it doesn’t seem to have any effect.

What I’m seeing is that even though I get this error, the context seems to be semi-valid. I can use it on another thread

egl.eglMakeCurrent(display, EGL10. EGL_NO_SURFACE, EGL10. EGL_NO_SURFACE, context);

After that, creating a texture on that thread does not cause an error. But I do see that texture names are not shared, and each thread seems to count from 0 itself.


My next hypothesis is that I need to share surfaces between contexts. However, if I go into my eglMakeCurrent from the original context through the same surface, but I fail completely

E/AndroidRuntime(3210): java.lang.IllegalArgumentException
E/AndroidRuntime(3210):     at com.google.android.gles_jni. EGLImpl._eglCreateContext(Native Method)
E/AndroidRuntime(3210):     at com.google.android.gles_jni. EGLImpl.eglCreateContext(EGLImpl.java:54)

I

felt as if I was almost there, did anyone know what was missing?

Solution

As a result, thanks to this Some help with question:

My secondary context needs a surface. It is not the same surface as the original context.

I need to create a new surface using eglCreatePbufferSurface. The reason I tried to do this earlier failed was because it defaulted to 0 width and height. By setting it to a 1×1 surface, it works perfectly.

egl.eglCreatePbufferSurface(display, config, new int[] { EGL10. EGL_WIDTH, 1, EGL10. EGL_HEIGHT, 1, EGL10. EGL_NONE });

Related Problems and Solutions