private static int getMaxTextureUnits() {
   IntBuffer buffer = BufferUtils.newIntBuffer(16);
   if (Gdx.graphics.isGL20Available())
     Gdx.gl.glGetIntegerv(GL20.GL_MAX_TEXTURE_IMAGE_UNITS, buffer);
   else Gdx.gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_UNITS, buffer);
   return buffer.get(0);
 }
  /** Releases all resources associated with the FrameBuffer. */
  public void dispose() {
    GL20 gl = Gdx.graphics.getGL20();

    IntBuffer handle = BufferUtils.newIntBuffer(1);

    colorTexture.dispose();
    if (hasDepth) {
      handle.put(depthbufferHandle);
      handle.flip();
      gl.glDeleteRenderbuffers(1, handle);
    }

    handle.clear();
    handle.put(framebufferHandle);
    handle.flip();
    gl.glDeleteFramebuffers(1, handle);

    if (buffers.get(Gdx.app) != null) buffers.get(Gdx.app).removeValue(this, true);
  }
 private static int getMaxTextureUnits() {
   IntBuffer buffer = BufferUtils.newIntBuffer(16);
   Gdx.gl.glGetIntegerv(GL20.GL_MAX_TEXTURE_IMAGE_UNITS, buffer);
   return buffer.get(0);
 }
  private void build() {
    if (!Gdx.graphics.isGL20Available()) throw new GdxRuntimeException("GL2 is required.");

    GL20 gl = Gdx.graphics.getGL20();

    // iOS uses a different framebuffer handle! (not necessarily 0)
    if (!defaultFramebufferHandleInitialized) {
      defaultFramebufferHandleInitialized = true;
      if (Gdx.app.getType() == ApplicationType.iOS) {
        IntBuffer intbuf =
            ByteBuffer.allocateDirect(16 * Integer.SIZE / 8)
                .order(ByteOrder.nativeOrder())
                .asIntBuffer();
        gl.glGetIntegerv(GL20.GL_FRAMEBUFFER_BINDING, intbuf);
        defaultFramebufferHandle = intbuf.get(0);
      } else {
        defaultFramebufferHandle = 0;
      }
    }

    setupTexture();

    IntBuffer handle = BufferUtils.newIntBuffer(1);
    gl.glGenFramebuffers(1, handle);
    framebufferHandle = handle.get(0);

    if (hasDepth) {
      handle.clear();
      gl.glGenRenderbuffers(1, handle);
      depthbufferHandle = handle.get(0);
    }

    gl.glBindTexture(GL20.GL_TEXTURE_2D, colorTexture.getTextureObjectHandle());

    if (hasDepth) {
      gl.glBindRenderbuffer(GL20.GL_RENDERBUFFER, depthbufferHandle);
      gl.glRenderbufferStorage(
          GL20.GL_RENDERBUFFER,
          GL20.GL_DEPTH_COMPONENT16,
          colorTexture.getWidth(),
          colorTexture.getHeight());
    }

    gl.glBindFramebuffer(GL20.GL_FRAMEBUFFER, framebufferHandle);
    gl.glFramebufferTexture2D(
        GL20.GL_FRAMEBUFFER,
        GL20.GL_COLOR_ATTACHMENT0,
        GL20.GL_TEXTURE_2D,
        colorTexture.getTextureObjectHandle(),
        0);
    if (hasDepth) {
      gl.glFramebufferRenderbuffer(
          GL20.GL_FRAMEBUFFER, GL20.GL_DEPTH_ATTACHMENT, GL20.GL_RENDERBUFFER, depthbufferHandle);
    }
    int result = gl.glCheckFramebufferStatus(GL20.GL_FRAMEBUFFER);

    gl.glBindRenderbuffer(GL20.GL_RENDERBUFFER, 0);
    gl.glBindTexture(GL20.GL_TEXTURE_2D, 0);
    gl.glBindFramebuffer(GL20.GL_FRAMEBUFFER, defaultFramebufferHandle);

    if (result != GL20.GL_FRAMEBUFFER_COMPLETE) {
      colorTexture.dispose();
      if (hasDepth) {
        handle.clear();
        handle.put(depthbufferHandle);
        handle.flip();
        gl.glDeleteRenderbuffers(1, handle);
      }

      colorTexture.dispose();
      handle.clear();
      handle.put(framebufferHandle);
      handle.flip();
      gl.glDeleteFramebuffers(1, handle);

      if (result == GL20.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
        throw new IllegalStateException(
            "frame buffer couldn't be constructed: incomplete attachment");
      if (result == GL20.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS)
        throw new IllegalStateException(
            "frame buffer couldn't be constructed: incomplete dimensions");
      if (result == GL20.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT)
        throw new IllegalStateException("frame buffer couldn't be constructed: missing attachment");
      if (result == GL20.GL_FRAMEBUFFER_UNSUPPORTED)
        throw new IllegalStateException(
            "frame buffer couldn't be constructed: unsupported combination of formats");
      throw new IllegalStateException(
          "frame buffer couldn't be constructed: unknown error " + result);
    }
  }