Search Unity

Graphics.blit() not working for android!!

Discussion in 'Shaders' started by Deleted User, Jun 6, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hii everyone,

    I have a query regarding Graphics.Blit() call for android. Here are the brief of the process:-
    We are using Graphics.Blit(Texture source, RenderTexture dest, Material mat) to copy data from source texture to destination texture with a shader. This call is working in editor mode & for windows platform. But, in android platform, we don't see any content in destination texture.
    We are making this call in Update() call.
    Any suggestions to make it work in android platform? Any feedback regarding this will be helpful.
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    857
    Please check the logcat output for errors first.
    There are multiple reasons this can be the case: the shader you use might not be supported on the device, or the render texture format, or something else.
     
    look001 likes this.
  3. Deleted User

    Deleted User

    Guest

    No errors are seen in logcat.
    To verify if the problem is with Render Texture I have created, I used Blit(Texture source, RenderTexture dest). With this call, I can confirm that content getting copied to dest.
    To verify if the problem is with the shader in android platform, I attached the shader to a material of cube(3D object asset in unity). With this, I can confirm that shader is doing its work properly in android platform.
    Problem is seen only when I use Blit(Texture source, RenderTexture dest, Material mat) to apply the shader while copying from source to destination texture & problem is only in android platform.

    Material mat = null;
    Shader alphaSet = null;
    RenderTexture rt = null;
    Texture tex = null;
    Texture2D tex2D = null;
    void Start()
    {
    Color32 testColor = new Color32(255, 0, 0, 255);

    alphaSet = Shader.Find("AlphaShader");
    if(alphaSet)
    {
    Debug.Log("Found Alpha Set Shader");
    mat = new Material(alphaSet);
    mat.shader = alphaSet;
    }
    else
    {
    Debug.LogError("Did not find Alpha Set Shader XXXXXXXXXX ");
    }
    tex2D = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
    tex2D.filterMode = FilterMode.Point;
    Color32[] testColorArray = tex2D.GetPixels32();
    tex2D.name = "sample_tex2d";

    for (int i = 0; i < testColorArray.Length; i++)
    {
    testColorArray = testColor;
    }

    tex2D.SetPixels32(testColorArray);
    tex2D.Apply();

    tex = tex2D;

    rt.filterMode = FilterMode.Point;
    rt.anisoLevel = 0;
    rt.antiAliasing = Mathf.Max(QualitySettings.antiAliasing, 1);
    rt.Create();
    }

    void Update()
    {
    Graphics.Blit(tex, rt, mat);
    }
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    857
    Can you please post the shader source as well?
    And the parameters you use to create the render texture as well.
     
  5. Deleted User

    Deleted User

    Guest



    Creating RenderTexture was the problem.
    We were using
    Code (CSharp):
    1.  rt = new RenderTexture(200, 100, 24, RenderTextureFormat.ARGB32);
    Changing it to
    Code (CSharp):
    1.  rt = new RenderTexture(200, 100, 0, RenderTextureFormat.ARGB32);
    worked.

    Not sure, how depth buffer size was affecting only in android platform. Any explanation?
    Information you requested made me look at the parameters passed to Render Texture.
    Thank you for the support.
     
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    857
    It's hard to say what exactly went wrong there. Perhaps you're using DirectX renderer on Windows and in the Editor, and something is different in the bit implementation.
    It would be nice if you could submit a bugreport so that we could investigate further.
     
  7. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    29
    I'm having a similar problem, Graphics.Blit giving me a black screen on an Android device but working fine in the Editor or on a Rift.
    The case is even simpler.
    I'm sure it is some kind of configuration error, I just can't figure out what.

    Code (CSharp):
    1.  
    2. private void Start()
    3.     {
    4.         SimpleShader = Shader.Find("Custom/Simple");
    5.         SimpleMaterial = new Material(SimpleShader);
    6.     }
    7. private void OnRenderImage(RenderTexture source, RenderTexture destination)
    8.     {
    9.         Graphics.Blit(source, destination, SimpleMaterial);
    10.     }
    11.  
    Code (CSharp):
    1.  
    2. Shader "Custom/Simple"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Main Texture", 2D) = "black"{} // The source texture passed into Graphics.Blit.
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "Queue" = "Overlay" "RenderType" = "Overlay" }
    11.         LOD 10
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16. #pragma vertex vert_img
    17. #pragma fragment frag
    18. #include "UnityCG.cginc"
    19.  
    20.             fixed4 frag(v2f_img i) : SV_TARGET
    21.             {
    22.                 return fixed4(0.6f, 0.5f, 0.3f, 1.0f);
    23.             }
    24.             ENDCG
    25.         }
    26.     }
    27.     FallBack Off
    28. }
    29.  
     
  8. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    29
    Got a little further and proved that a simple blit without material works fine but that the blit with material does nothing by first doing a copy with a simple blit like so:
    Code (CSharp):
    1.         Graphics.Blit(source, destination);
    2.         // next blit doesn't alter anything to destination
    3.         Graphics.Blit(source, destination, SimpleMaterial);
     
    Last edited: Feb 1, 2019
  9. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    Maybe it will help:

    Code (CSharp):
    1.     void Blit(RenderTexture source, RenderTexture destination, Material mat)
    2.     {
    3.         RenderTexture.active = destination;
    4.         mat.SetTexture("_MainTex", source);
    5.         GL.PushMatrix();
    6.         GL.LoadOrtho();
    7.         GL.invertCulling = true;
    8.         mat.SetPass(0);
    9.         GL.Begin(GL.QUADS);
    10.         GL.MultiTexCoord2(0, 0.0f, 0.0f);
    11.         GL.Vertex3(0.0f, 0.0f, 0.0f);
    12.         GL.MultiTexCoord2(0, 1.0f, 0.0f);
    13.         GL.Vertex3(1.0f, 0.0f, 0.0f);
    14.         GL.MultiTexCoord2(0, 1.0f, 1.0f);
    15.         GL.Vertex3(1.0f, 1.0f, 1.0f);
    16.         GL.MultiTexCoord2(0, 0.0f, 1.0f);
    17.         GL.Vertex3(0.0f, 1.0f, 0.0f);
    18.         GL.End();
    19.         GL.invertCulling = false;
    20.         GL.PopMatrix();      
    21.     }
     
  10. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    119
  11. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    857
    @BakeMyCake no, this is a different thing. This controls whether rendering is done to a temporary render target before being presented to the screen instead of being rendered directly to the framebuffer.

    @fortgreeneVR please submit a bug report.
     
  12. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    29
    Update: Graphics.Blit works fine with XR setting Multi Pass but not with Single Pass (at least not in 2018.3.3)
    I'll test with the latest and submit a bug report if still present.
     
  13. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    29
    Filed case 1123989, thanks for looking into it.
     
  14. scotthNM

    scotthNM

    Joined:
    Apr 17, 2015
    Posts:
    3
    Was there ever a fix for this one? I'm seeing it in 2019.1.0b1 on PC Standalone (DX11).
     
  15. kevin_irisvr

    kevin_irisvr

    Joined:
    Jun 3, 2016
    Posts:
    1
    Note for people looking at this thread. I noticed the following documentation about needing to modify shaders for SinglePass. This might be relevant depending on what you are doing.

    Look for section on

    Graphics.Blit()
    ...

    https://docs.unity3d.com/Manual/SinglePassStereoRendering.html
     
  16. Aidan-Wolf

    Aidan-Wolf

    Joined:
    Jan 6, 2014
    Posts:
    45
    Graphics.Blit() works fine on _any_ Google Pixel phone, doesn't work on _any_ Samsung phone.

    Explanation?
     
  17. Raveler

    Raveler

    Joined:
    Sep 24, 2012
    Posts:
    36
    I have the same problem, with Unity 2019.1.9f1. It is only broken with OpenGLES2, not with Vulkan. This might be relevant as well.