title: Opengl GL_QUADS produces error 0x506
body: <p>I have some code in OpenGL to render a YUV image onto an OpenGL viewport. The program works without a problem when running on nvidia cards, but it generates an error when running over the Intel HD 3000, which sadly is the target machine. The point where the error is generated is marked in the code.</p>
  
  <p>The shader programs are</p>
  
  <pre><code>// Vertex Shader
  #version 120
  void main() {
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  }
  
  // fragment shader
  #version 120
  uniform sampler2D texY;
  uniform sampler2D texU;
  uniform sampler2D texV;
  void main() {
    vec4 color;
    float y = texture2D(texY, gl_TexCoord[0].st).r;
    float u = texture2D(texU, gl_TexCoord[0].st).r;
    float v = texture2D(texV, gl_TexCoord[0].st).r;
    color.r = (1.164 * (y - 0.0625)) + (1.596 * (v - 0.5));
    color.g = (1.164 * (y - 0.0625)) - (0.391 * (u - 0.5)) - (0.813 * (v - 0.5));
    color.b = (1.164 * (y - 0.0625)) + (2.018 * (u - 0.5));
    color.a = 1.0;
    gl_FragColor = color;
  };
  </code></pre>
  
  <p>Then I run the program like this:</p>
  
  <pre><code>GLuint textures[3];
  glGenTextures(3, textures);
  
  glBindTexture(GL_TEXTURE_2D, textures[YTEX]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
  glBindTexture(GL_TEXTURE_2D, textures[UTEX]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
  glBindTexture(GL_TEXTURE_2D, textures[VTEX]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
  glBindTexture(GL_TEXTURE_2D, 0);
  
  GLsizei size = width * height;
  
  GLvoid *y = yuv_buffer;
  GLvoid *u = (GLubyte *)y + size;
  GLvoid *v = (GLubyte *)u + (size &gt;&gt; 2);
  
  glUseProgram(program_id);
  
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, textures[0]);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE,
              GL_UNSIGNED_BYTE, y);
  glUniform1i(glGetUniformLocation(program_id, "texY"), 0);
  
  glActiveTexture(GL_TEXTURE1);
  glBindTexture(GL_TEXTURE_2D, textures[1]);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width &gt;&gt; 1, height &gt;&gt; 1, 0,
              GL_LUMINANCE, GL_UNSIGNED_BYTE, u);
  glUniform1i(glGetUniformLocation(program_id, "texU"), 1);
  
  glActiveTexture(GL_TEXTURE2);
  glBindTexture(GL_TEXTURE_2D, textures[2]);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width &gt;&gt; 1, height &gt;&gt; 1, 0,
              GL_LUMINANCE, GL_UNSIGNED_BYTE, u);
  glUniform1i(glGetUniformLocation(program_id, "texV"), 2);
  
  glBegin(GL_QUADS);
  glTexCoord2f(texLeft, texTop);
  glVertex2i(left, top);
  glTexCoord2f(texLeft, texBottom);
  glVertex2i(left, bottom);
  glTexCoord2f(texRight, texBottom);
  glVertex2i(right, bottom);
  glTexCoord2f(texRight, texTop);
  glVertex2i(right, top);
  glEnd();
  
  // glError() returns 0x506 here
  
  glBindTexture(GL_TEXTURE_2D, 0);
  glActiveTexture(GL_TEXTURE0);
  
  glUseProgram(0);
  </code></pre>
  
  <p><strong>update</strong> since the error happens with frame buffers, I discover they are used like this:
  when the program is instantiated, a frame buffer is created like this:</p>
  
  <pre><code>glViewport(0, 0, (GLint)width, (GLint)height);
  
  glGenFramebuffers(1, &amp;fbo_id);
  glGenTextures(1, &amp;fbo_texture);
  glGenRenderbuffers(1, &amp;rbo_id);
  
  glBindTexture(GL_TEXTURE_2D, fbo_texture);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
              GL_RGBA, GL_UNSIGNED_BYTE, 0);
  glBindTexture(GL_TEXTURE_2D, 0);
  
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rbo_id);
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                            GL_TEXTURE_2D, fbo_texture, 0);
  glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
                              GL_RENDERBUFFER_EXT, rbo_id);
  
  GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
  
  glPushAttrib(GL_TEXTURE_BIT);
  
  glBindTexture(GL_TEXTURE_2D, m_frameTexture-&gt;texture());
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
  glPopAttrib();
  </code></pre>
  
  <p>The YUV image comes spliced in tiles, which are assembled by rendering in this fbo. Whenever a frame starts, this is performed:</p>
  
  <pre><code>glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
  glDrawBuffer(GL_BACK);
  
  glViewport(0, 0, (GLint)width, (GLint)height);
  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, (double)width, 0.0, (double)height, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_id);
  </code></pre>
  
  <p>Then the code above is executed, and after all the tiles had been assembled together</p>
  
  <pre><code>glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
  glPushAttrib(GL_VIEWPORT_BIT | GL_TEXTURE_BIT | GL_ENABLE_BIT);
  glViewport(0, 0, width, height);
  
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT);
  
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  
  glLoadIdentity();
  glOrtho(0.0, (double)width, 0.0, (double)height, -1.0, 1.0);
  
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();
  
  glEnable(GL_TEXTURE_2D);
  glDisable(GL_DEPTH_TEST);
  
  glBindTexture(GL_TEXTURE_2D, fbo_texture);
  
  glBegin(GL_QUADS);
  glTexCoord2i(0, 0);
  glVertex2f(renderLeft, renderTop);
  glTexCoord2i(0, 1);
  glVertex2f(renderLeft, renderTop + renderHeight);
  glTexCoord2i(1, 1);
  glVertex2f(renderLeft + renderWidth, renderTop + renderHeight);
  glTexCoord2i(1, 0);
  glVertex2f(renderLeft + renderWidth, renderTop);
  glEnd();
  
  glPopMatrix();
  
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glPopAttrib();
  </code></pre>