glGenTextures在后台线程中返回零

Div*_*ion 7 iphone multithreading opengl-es

我需要在OpenGL ES中的后台线程中加载纹理.但是glGenTextures在后台线程中调用时总是返回零.

-(void) someMethodInMainThread {
   [self performSelectorInBackground:@selector(load) withObject:nil];
}

-(void) load {
   GLuint textureID = 0;
   glGenTextures(1, &textureID);        
}
Run Code Online (Sandbox Code Playgroud)

textureID为零.如果我将代码更改为[self performSelector:@selector(tmp)withObject:nil]; 它将正常工作并返回1.如何在后台线程中加载纹理?

Mat*_*gro 10

这是一个常见错误,每个OpenGL上下文只能在一个线程中处于活动状态(当前),因此当您创建新线程时,它没有任何OpenGL上下文,并且所有GL调用都会失败.

解决方案:创建另一个OpenGL上下文,使其在后台线程中保持最新.要加载纹理,您还需要与主上下文共享OpenGL名称(纹理ID等).