views:

921

answers:

2

Here's the code I use to load a texture. image is a CGImageRef. After loading the image with this code, I eventually draw the image with glDrawArrays().

 size_t imageW = CGImageGetWidth(image);
 size_t imageH = CGImageGetHeight(image);
 size_t picSize = pow2roundup((imageW > imageH) ? imageW : imageH);

 GLubyte *textureData = (GLubyte *) malloc(picSize * picSize << 2);
 CGContextRef imageContext = CGBitmapContextCreate( textureData, picSize, picSize, 8, picSize << 2, CGImageGetColorSpace(image), kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big ); 

 if (imageContext != NULL) {
  CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, (CGFloat)imageW, (CGFloat)imageH), image); 
  glGenTextures(1, &textureId); 
  glBindTexture(GL_TEXTURE_2D, textureId);

  // when texture area is small, bilinear filter the closest mipmap
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  // when texture area is large, bilinear filter the original
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  // the texture wraps over at the edges (repeat)
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, picSize, picSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

  GLenum err = glGetError();
  if (err != GL_NO_ERROR)
   NSLog(@"Error uploading texture. glError: 0x%04X", err);

  CGContextRelease(imageContext);
 }

 free(textureData);

This seems to work fine when the image is 320x480, but it fails when the image is larger (for example, picSize = 2048).

Here's what I get in the Debugger Console:

Error uploading texture. glError: 0x0501

What's the meaning of this error? What's the best workaround?

A: 

Maybe you are simply running out of memory - did you verify the value returned from malloc() is not NULL?
(this could explain getting 0x0501, which is GL_INVALID_VALUE).

Hexagon
+5  A: 

Aren’t you simply hitting the maximum texture size limit? The error code is GL_INVALID_VALUE, see the glTexImage2D docs:

GL_INVALID_VALUE is generated if width or height is less than 0 or greater than 2 + GL_MAX_TEXTURE_SIZE, or if either cannot be represented as 2k+2(border) for some integer value of k.

iPhone does not support textures larger than 1024 pixels. The workaround is to split the image into several textures.

zoul
Sounds like a more likely answer than mine...
Hexagon