opengl-es-2.0fragment-shadertexture-mappingtexture2doff-screen

Android: Negative values for offscreen texture gets clamped to 0


I am using an offscreen rendering to a 2D texture. My first shader will output YUV values and second fragment shader reads YUV from the offscreen texture. In the second shader, I manipulate U and V and make it between -0.5 to 0.5 range. But these get clamped to 0 to 1 (positive) as soon as I try to render it. All my negative values become 0.

Found the issue to be here :

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA,
                width, height, 0, GLES20.GL_RGBA,
                GLES20.GL_UNSIGNED_BYTE, null)

If I use GL_FLOAT instead of GL_UNSIGNED_BYTE, it will give me error as color format is not matching. From the link, https://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml looks like all supported formats will clamp to 0-1 range. How to get negative values also in next texture.

Found that I can use other image formats like GL_R16F but none of them are supported in the link / in GLES20.


Solution

  • Would you be better off continuing to use GL_UNSIGNED_BYTE but just remap the UV values from the -1 to 1 range to the 0 to 1 range before writing them in the first shader. Then after reading them in the second shader, you map back from the 0 to 1 range to the -1 to 1 range.

    It's only an extra multiply-add in each of your shaders and would save a significant amount of texture memory/bandwidth.

    e.g. End of shader 1

    vShader1Output = vUVBetweenNeg1And1 * vec2(0.5, 0.5) + vec2(0.5, 0.5);
    

    e.g. Start of shader 2

    vShader2Input = vUVFromShader1Texture * vec2(2.0, 2.0) - vec2(1.0, 1.0)