Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glGenTextures Returning Existing Texture Name

I'm on iOS 6.1.4, OpenGL ES 2, and am experiencing strange behavior with glGenTextures returning texture "names" which have previously been returned by glGenTextures.

Specifically, at initialization time, I iterate through my textures and make OpenGL aware of them, like this:

// Generate the OpenGL texture objects
for (Object3D *object3D in self.objects3D)
{
    [self installIntoOpenGLObject3D:object3D];
}

- (void)installIntoOpenGLObject3D:(Object3D *)object3D
{
    GLuint nID;
    glGenTextures(1, &nID);

    Texture *texture = object3D.texture;
    texture.identifier = nID;
    glBindTexture(GL_TEXTURE_2D, nID);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [texture width], [texture height], 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)[texture pngData]);
    ARLogInfo(@"Installed texture '%@' with OpenGL identifier %d", texture.name, texture.identifier);
}

At this time, I see texture names like 1, 2, 3, ... n as expected.

However, sometime later (after an asynchronous network call returns) I create new textures and call installIntoOpenGLObject3D: to install them. It is at this later time when I see glGenTextures returning names which were issued previously. Obviously this causes the app to render the incorrect texture.

The docs, http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGenTextures.xml , suggest that:

Texture names returned by a call to glGenTextures are not returned by subsequent calls, unless they are first deleted with glDeleteTextures.

and I'm making no calls to glDeleteTextures; however the docs also say:

it is guaranteed that none of the returned names was in use immediately before the call to glGenTextures.

which is confusing, since it implies that the returned names could be the same if they were not "in use." To my understanding, by calling glBindTexture for the texture satisfies the "in use" notion, but, something is wrong.

Another thought I had was perhaps the EAGLContext was changing somewhere between the first set of calls to glGenTextures but, alas, stepping through in the debugger tells me the context is not changing.

As a (perhaps foolish) hackish workaround I attempted to ensure the texture names were unique myself, as you can see below, however I simply end up with a texture name which I do not track, but also still represents a different texture, so this approach does not provide a workaround:

- (void)installIntoOpenGLObject3D:(Object3D *)object3D
{
    if (!self.object3DTextureIdentifiers)
    {
        self.object3DTextureIdentifiers = [NSMutableSet set];
    }

    GLuint nID;
    NSNumber *idObj;
    do {
        glGenTextures(1, &nID);
        GLboolean isTexture = glIsTexture(nID);
        idObj = [NSNumber numberWithUnsignedInt:nID];
    } while ([self.object3DTextureIdentifiers containsObject:idObj]);

    [self.object3DTextureIdentifiers addObject:idObj];

    Texture *texture = object3D.texture;
    texture.identifier = nID;
    glBindTexture(GL_TEXTURE_2D, nID);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [texture width], [texture height], 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)[texture pngData]);
    ARLogInfo(@"Installed texture '%@' with OpenGL identifier %d", texture.name, texture.identifier);
}

Any thoughts on why glGenTextures is returning names which it previously returned?

like image 216
levigroker Avatar asked Oct 21 '22 07:10

levigroker


1 Answers

After much debugging, I believe the root cause to be threading.

While I was certain glGenTextures was only being called from one thread (main), it turns out that calls to other OpenGL methods were being called on a different thread. Refactoring so the calls to glGenTextures also occur only on that other thread (not-main) seems to address the issue.

like image 130
levigroker Avatar answered Oct 24 '22 13:10

levigroker