title: Android: Rendering with SurfaceTexture to FrameBuffer
body: <p>I am working on an effect that requires dual-pass rendering (i.e. two separate shaders are applied in succession). I am using a GLSurfaceView with a custom renderer.</p>
  
  <p>In order to achieve this effect, I am trying to render to a FrameBuffer first, and then to a GL_TEXTURE_2D for display in the GLSurfaceView. </p>
  
  <p>The relevant code in the OnSurfaceCreated method is the following:</p>
  
  <pre><code>        GLES20.glGenTextures(1, this.textureID, 0);
          GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES,this.textureID[0]);
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
          GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
  
          GLES20.glGenTextures(1, textures2D, 0);
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures2D[0]);
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
  
          GLES20.glGenFramebuffers(1, fboId, 0);
          GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId[0]);
          GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, textures2D[0], 0);
          GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
  </code></pre>
  
  <p>The relevant code in OnDrawFrame is this:</p>
  
  <pre><code>       synchronized (this) {
              if (this.updateSurface) {
                  this.surfaceTexture.updateTexImage();
                  this.surfaceTexture.getTransformMatrix(this.stMatrix);
                  this.updateSurface = false;
              }
          }
  
          GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
          GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
  
          GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId[0]);
          GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, this.textureID[0]);
  
  // apply the first shader
  
          GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures2D[0]);
  
  // apply the second shader
  
          GLES20.glFinish();
  </code></pre>
  
  <p>However, the program is crashing due to glError 1286, which seems to indicate an invalid framebuffer operation. I am not sure what is causing this issue, though I suspect it might have to do with the fact that the SurfaceTexture is associated with a GL_TEXTURE_EXTERNAL_OES texture, while the FrameBuffer is outputting a GL_TEXTURE_2D texture.</p>
  
  <p>Any help or advice would be appreciated. Thank you.</p>