Can't call glGenTextures on multithreaded android app

前端 未结 4 464
灰色年华
灰色年华 2021-01-15 14:14

I\'m making an OpenGLES Android app using Android NDK, expanding from android\'s gljni example, which can be found here

It\'s using GLSurfaceView. Textures are initi

相关标签:
4条回答
  • 2021-01-15 14:51

    It's not random behaviour, that's how OpenGL interacts with threads. A context is only current on only ONE thread, other threads don't have a GL context unless you specifically create a context for each thread you want to use with OpenGL. Without a context, all GL calls fail.

    0 讨论(0)
  • 2021-01-15 15:01

    I described a java-only solution for uploading textures on a separate thread as an answer to another question: Threading textures load process for android opengl game

    It should work fairly similar with the NDK.

    0 讨论(0)
  • 2021-01-15 15:02

    I haven't worked with the NDK and OpenGL. But with the pure Java version, you can not share threads. GLSurfaceView doesn't like sharing GL contexts between threads. The reason for this (from what I have been able to tell) is that after a drawFrame() call, the context gets lost. If you try to use that context while not inside onSurfaceCreated, onSurfaceChanged, or onDrawFrame(), your GL methods won't work. Therefore, with a different thread there is a high chance that when that other thread executes, the GL thread has already finished its drawFrame() method, invalidating the context.

    0 讨论(0)
  • 2021-01-15 15:02

    I got it working using GLSurfaceView.queueEvent().

    The document says that GLSurfaceView is careful of separating UI thread and rendering thread, and

    queueEvent() makes a code run in its rendering thread.

    0 讨论(0)
提交回复
热议问题