Using Rendertexture on Android Native JAR

Hello, I’m trying to render an image using GLES in an Android Java library. When I try to clear the color buffer or attempt to draw to the binded rendertexture, I get a glerror 1286. From what I researched, this error is a result of an invalid framebuffer being used. Are unity rendertextures the same as a framebuffer? How should I use a GL_TEXTURE_EXTERNAL_OES texture in Unity?

Here’s what I have so far:

Java:

        public void drawFrame(SurfaceTexture st, boolean invert, int renderTexture) {
            //bind to unity rendertexture
            GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, renderTexture);
            checkGlError("glBindFramebuffer");
            GLES20.glViewport(0,0, 512, 512);
            checkGlError("glViewport");
            GLES20.glDisable(GLES20.GL_DEPTH_TEST);
            checkGlError("onDrawFrame start");
            st.getTransformMatrix(mSTMatrix);
            if (invert) {
                mSTMatrix[5] = -mSTMatrix[5];
                mSTMatrix[13] = 1.0f - mSTMatrix[13];
            }

            GLES20.glUseProgram(mProgram);
            checkGlError("glUseProgram");

            //binding the gl texture oes from an android surfacetexture
            GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
            GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
            mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
            GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
                    TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
            checkGlError("glVertexAttribPointer maPosition");
            GLES20.glEnableVertexAttribArray(maPositionHandle);
            checkGlError("glEnableVertexAttribArray maPositionHandle");

            mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
            GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false,
                    TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
            checkGlError("glVertexAttribPointer maTextureHandle");
            GLES20.glEnableVertexAttribArray(maTextureHandle);
            checkGlError("glEnableVertexAttribArray maTextureHandle");

            Matrix.setIdentityM(mMVPMatrix, 0);
            GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
            GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);

            //draw the surfacetexture to the rendertexture returning glError 1286
            GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
            checkGlError("glDrawArrays");

            GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
            GLES20.glEnable(GLES20.GL_DEPTH_TEST);
            //unbind rendertexture
            GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        }

Unity C#:

		if (!textureTarget)
		{
			textureTarget = new RenderTexture(512, 512, 16, RenderTextureFormat.ARGB32);
			textureTarget.filterMode = FilterMode.Point;
			textureTarget.wrapMode = TextureWrapMode.Clamp;
			textureTarget.Create();
			if (material) material.sharedMaterial.mainTexture = textureTarget;
		}
		//update surfacetexture
		exoPlayer.Call("updateTexture");
		//draw to rendertexture
		exoPlayer.Call("drawFrame", (int)textureTarget.GetNativeTexturePtr());

You’ll need to adjust the shader to use the GL_TEXTURE_EXTERNAL_OES texture as well, else it won’t work. Add to your shader instead of sampler2D:

 #extension GL_OES_EGL_image_external : require
uniform samplerExternalOES texture