glGenTextures在后台线程中返回零 [英] glGenTextures returns zero in background thread
问题描述
我需要在OpenGL ES中的后台线程中加载纹理。但是glGenTextures在后台线程中调用时总是返回零。
I need to load textures in background thread in OpenGL ES. But glGenTextures always returns zero when called in background thread.
-(void) someMethodInMainThread {
[self performSelectorInBackground:@selector(load) withObject:nil];
}
-(void) load {
GLuint textureID = 0;
glGenTextures(1, &textureID);
}
textureID为零。
如果我将代码更改为[self performSelector:@selector(tmp)withObject:nil];它将正常工作并返回1.
如何在后台线程中加载纹理?
textureID is zero. If i change code to [self performSelector:@selector(tmp) withObject:nil]; it will work correct and return 1. How should i load textures in background thread?
推荐答案
这是一个常见的错误,每个OpenGL上下文只能在一个线程中处于活动状态(当前),因此当您创建新线程时,它没有任何OpenGL上下文,并且所有GL调用都失败。
This is a common error, each OpenGL context can be active (current) in one thread only, so when you create a new thread, it doesn't have any OpenGL context, and all GL calls fail.
解决方案:创建另一个OpenGL上下文,使其在后台线程中保持最新状态。要加载纹理,您还需要与主要上下文共享OpenGL名称(纹理ID等)。
Solution: Create another OpenGL context, make it current in your background thread. To load textures, you also want to share OpenGL names (texture ids, etc) with the main context.
这篇关于glGenTextures在后台线程中返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!