2

Why is glTexSubImage2D() suddenly causing GL_INVALID_OPERATION?

I'm trying to upgrade my hopelessly outdated augmented reality app from iOS4.x to iOS5.x, but I'm having difficulties. I run iOS5.0. Last week I ran iOS4.3. My device is an iPhone4.


Here is a snippet from my captureOutput:didOutputSampleBuffer:fromConnection: code

uint8_t *baseAddress = /* pointer to camera buffer */
GLuint texture = /* the texture name */
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 480, 360, GL_BGRA, GL_UNSIGNED_BYTE, baseAddress);
/* now glGetError(); -> returns 0x0502 GL_INVALID_OPERATION on iOS5.0, works fine on iOS4.x */

Here is a snippet from my setup code

GLuint texture = /* the texture name */
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

For simplicity I have inserted hardcoded values here. In my actual code I obtain these values with CVPixelBufferGetWidth/Height/BaseAddress. The EAGLContext is initialized with kEAGLRenderingAPIOpenGLES2.

neoneye
  • 50,398
  • 25
  • 166
  • 151

1 Answers1

6

Ah.. I fixed it immediately after posting this question. Had to change GL_RGBA into GL_BRGA.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);

Hope it helps someone.

BTW. If you want to write AR apps then consider using CVOpenGLESTextureCache instead of using glTexSubImage2d. It's supposed to be faster.

neoneye
  • 50,398
  • 25
  • 166
  • 151
  • 1
    On iOS 5.0, the texture cache is much faster. It took my iPhone 4S video frame upload and processing times from 9.0 ms to 1.8 ms. I describe how I integrated this in a framework of mine within this answer: http://stackoverflow.com/a/9574798/19679 – Brad Larson Mar 10 '12 at 16:56
  • THANK YOU!!! before I had glTexImage2D(..., GL_BGRA, ..., GL_BGRA, ...) which used to work. Switching the first one to GL_RGBA fixed the problem (the glTexSubImage2D is still using GL_BGRA). – Angus Forbes Apr 30 '12 at 08:59
  • 2
    can someone explain why glTexSubImage2D needs GL_BGRA instead of GL_RGBA???? this just helped me out a ton, but don't understand why it's different. – joshue Sep 16 '12 at 18:25
  • Awesome!!! I had the same problem too (glTexImage2D giving GL_INVALID_OPERATION). Or rather, someone else had this problem and I had to fix it for them. I rarely touch OpenGL code so this was a life saver. :-) – Jonny Oct 23 '12 at 14:17