Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLKTextureLoader fails when loading a certain texture the first time, but succeeds the second time

I'm making an iPhone application with OpenGL ES 2.0 using the GLKit. I'm using GLKTextureLoader to load textures synchronously.

The problem is that for a certain texture, it fails to load it the first time. It gives this error:

The operation couldn’t be completed. (GLKTextureLoaderErrorDomain error 8.)

For this error code, the apple documentation says the following:


GLKTextureLoaderErrorUncompressedTextureUpload
An uncompressed texture could not be uploaded.
Available in iOS 5.0 and later.
Declared in GLKTextureLoader.h.

(not very much).

Could I be trying to load the texture while the opengl context is in some busy state or something like that?

Notes:

  • Before getting to load this texture I load other textures and those work on the first try. Also, the exact same texture file will load ok on the second try.
  • There should be enough free video memory as I have only a couple of textures loaded before this one.
  • The texture is an uncompressed PNG with alpha, but I also tried with TGA (24bit & 32bit) with no luck.

Any ideas are welcomed, thanks

EDIT:

More info:

  • the opengl context is shared between all my screens. I'm doing this to keep my shaders and textures loaded between screens.

  • the problem above happens when I go to my second screen. In the first screen I draw textured stuff with no problems (other textures though).

  • The problem above happens when I load my content (game entities) in the game world. Each entity tries to load the texture. I have a simple caching system that loads the texture only once and then returns the same id for all other entities. I'm loading the entities synchronously, in one method. The first entity fails to load the texture then comes the second and succeeds and then the third one gets the cached id.

  • I am calling the load entities method in viewDidAppear and I've tried to add a sleep for 2 seconds before I load any entities but nothing changed.

EDIT:

Texture loading code:


- (GLKTextureInfo *)loadTextureAtPath:(NSString*)path ofType:(NSString*)type withKey:(NSString *)key 
{
    GLKTextureInfo* tex;

    tex = [self textureWithKey:key];
    if (tex)
        return tex;

    NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithBool:NO],
                              GLKTextureLoaderOriginBottomLeft, 
                              nil];

    NSError * error;    
    NSString *bundlepath = [[NSBundle mainBundle] pathForResource:path ofType:type];

    tex = [GLKTextureLoader textureWithContentsOfFile:bundlepath options:options error:&error];
    if (tex == nil) 
        DLOG_LOCAL(@"Error loading texture: %@", [error localizedDescription]);                
    else
        [textures setObject:tex forKey:key];

    return tex;
}


like image 831
Andrei Stanescu Avatar asked Dec 23 '11 00:12

Andrei Stanescu


2 Answers

I was also getting

The operation couldn’t be completed. (GLKTextureLoaderErrorDomain error 8.)

when loading a texture late in runtime while several previous textures had loaded successfully closer to launch. I was able to solve the problem by inserting the following line of code before the GLKTextureLoader call:

NSLog(@"GL Error = %u", glGetError());

Sure enough, GL was reporting an error, but it did not require me to address the error in order for GLKTextureLoader to work. Merely getting the GL Error was enough.

like image 58
Newman Avatar answered Oct 18 '22 01:10

Newman


I got this when enabling textures before loading the texture. Simply moved glEnable(GL_TEXTURE) after the loading and the issue was gone.

like image 4
Brett Avatar answered Oct 18 '22 00:10

Brett