Search Unity

shader not working as intended after transitioning from OpenGL ES 2 to OpenGL ES 3 on Android device

Discussion in 'Shaders' started by MaeL0000, Feb 9, 2018.

  1. MaeL0000


    Aug 8, 2015
    So I have a Unity plugin for an Android app split into two parts written in c++ (generating two separate .so files ).
    For discussion purposes I'm going to redact and simplify what they do:

    first what my c# script does:

    Code (CSharp):
    1. colorTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);
    2. externalPluginA.sendID(colorTexture.GetNativeTextureID());
    3. alphaTexture = new Texture2D(width, height, TextureFormat.Alpha8, false);
    4. externalPluginB.sendID(alphaTexture.GetNativeTextureID());
    now for what externalPluginA does with the first pointer:

    Code (C++):
    1. glBindTexture(GL_TEXTURE_2D, textureID);
    2. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, glPixFormat, glPixType, frameBuffer->buff);
    (frameBuffer->buffer is the raw output of the camera, but we can just assume it's a colored image. I can't check what glPixFormat and glPixFormat are at runtime but that should be irrelevant as you will soon see.)

    and what externalPluginB does with the second pointer:

    Code (C++):
    1. static std::vector<unsigned char> emptyPixelsAlpha(height * width, 0);
    3. glBindTexture(GL_TEXTURE_2D, alphaID);
    4. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_ALPHA, GL_UNSIGNED_BYTE,;
    this just fills the alpha channel of this texture with zeros, interpretable as a fully transparent alpha channel.

    colorTexture and alphaTexture are then fed to a material that has this shader:

    Code (shader):
    1.  Shader "alphaMaskShader"
    2. {
    3.     Properties{
    4.       _MainTex("Base (RGB)", 2D) = "white" {}
    5.       _Alpha("Alpha (A)", 2D) = "white" {}
    6.     }
    7.     SubShader{
    8.         Tags{ "RenderType" = "Transparent" "Queue" = "Overlay" }
    10.         ZWrite Off
    11.         ZTest Off
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         ColorMask RGB
    16.         Pass{
    17.             SetTexture[_MainTex]{
    18.             Combine texture
    19.             }
    20.             SetTexture[_Alpha]{
    21.             Combine previous, texture
    22.             }
    23.         }
    24.     }
    25. }
    what this does is simply use alphatexture as the alpha channel for colorTexture, and given that we filled alphaTexture with black it will give as output a fully transparent texture.

    The fact is this worked perfectly well up until I decided to switch from OpenGL ES 2 to OpenGL ES 3.
    To transition from one to the other I set OpenGL ES 3 as my Graphics API on Unity and in my two plugins I included GLES/gl3.h headers instead of GLES/gl2.h. The device I'm building on is a OnePlus 3 which supports OpenGL ES 3.

    what it does now is show the colorTexture fully opaque, as if the alphaTexture were set to opaque. I don't see any particular errors in the log any it isn't crashing or anything.
    The funny thing is that I decided to open up GAPID and record a trace, and lo and behold the frames shown in GAPID when analyzing the trace are displaying correct functioning, with the texture being completely transparent. I have no clue how this is possible and therefore ask for your help in figuring this out.