AkademiksQc
AkademiksQc

Reputation: 698

Objective-c : Most efficient way to load a texture to OpenGL

Currently i load a texture in iOS using Image I/O and I extract its image data with Core Graphics. Then i can send the image data to OpenGL like this :

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width, texture->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->imageData);

The problem is that the Core Graphics part is really slow, i need to setup and draw with Core Graphics just to extract the image data...i don't want to show it on screen. There must be a more efficient way to extract image data in iOS?...

Here is my code :

...
myTexRef = CGImageSourceCreateWithURL((__bridge CFURLRef)url, myOptions);
...
MyTexture2D* texture;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

void *imageData = malloc( tileSize.width * tileSize.height * 4 );

CGContextRef imgContext = CGBitmapContextCreate( imageData, tileSize.width, tileSize.height, 8, 4 * tileSize.width, colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease( colorSpace );
CGContextClearRect( imgContext, CGRectMake( 0, 0, tileSize.width, tileSize.height ) );
CGContextTranslateCTM( imgContext, 0, 0 );
...
CGImageRef tiledImage = CGImageCreateWithImageInRect (imageRef, tileArea);
CGRect drawRect = CGRectMake(0, 0, tileSize.width, tileSize.height);

// *** THIS CALL IS REALLY EXPENSIVE!
CGContextDrawImage(imgContext, drawRect, tiledImage);

CGImageRelease(tiledImage);

// TamTexture2D takes the ownership of imageData and will be responsible to free it
texture = new MyTexture2D(tileSize.width, tileSize.height, imageData);

CGContextRelease(imgContext);

Upvotes: 2

Views: 1697

Answers (2)

Adam
Adam

Reputation: 33146

GLKTextureLoader is orders of magnitude slower than using CG.

I have logged this as a bug with Apple DTS, and got it thrown back as "yes, we know, don't care, not going to fix it. You should be using CoreGraphics instead if you want your textures to be loaded fast" (almost that wording)

glTexImage2D is immensely fast if you give it the raw buffer that CGContext* methods create for you / allow you to pass in. Assuming you get the RGBA/ARGB/etc colour-spaces correct, of course.

cgcontextDrawImage is also super fast. My guess is that it's taking time to load your data over the web...

Upvotes: 1

MrMage
MrMage

Reputation: 7496

If you are developing for iOS 5 and above, GLKTextureLoader is what you're looking for:

Upvotes: 1

Related Questions