Search Unity

[Solved] Compute shader on Android not working

Discussion in 'Shaders' started by Frenchie92, Jan 24, 2020.

  1. Frenchie92

    Frenchie92

    Joined:
    Jul 4, 2013
    Posts:
    10
    I've spent the last couple days trying to figure out why my compute shader isn't working on Android. I've narrowed it down to the `tex2dlod` call always returning 0. Here's a minimal example:

    Code (CSharp):
    1. #pragma kernel NotWorking
    2.  
    3. RWTexture2D<float4> result;
    4.  
    5. float tex_width;
    6. float tex_height;
    7. sampler2D input;
    8.  
    9. [numthreads(8,8,1)]
    10. void NotWorking (uint3 id : SV_DispatchThreadID) {
    11.     float2 uv = float2(id.x / tex_width, id.y / tex_height);
    12.     float4 sample = tex2Dlod(input, float4(uv.x, uv.y, 0, 0));
    13.     result[id.xy] = sample;
    14. }
    On desktop, the output is the `result` texture being a copy of the `input` texture. As soon as I build/deploy to Android, `result` is just a blank (black) texture. I know the compute shader is getting executed because if I remove the `tex2Dlod` and replace it with a constant, I get the expected result on both platforms. EDIT: Turns out `tex2Dlod` wasn't to blame. I had replaced it with float4(1,1,1,1) and that was working but other values were not. Solution below.

    What am I doing wrong?!
     
    Last edited: Jan 26, 2020
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Hi!
    Can you please file a bug report?
    Thanks!
     
  3. Frenchie92

    Frenchie92

    Joined:
    Jul 4, 2013
    Posts:
    10
  4. Frenchie92

    Frenchie92

    Joined:
    Jul 4, 2013
    Posts:
    10
    I spent a long time playing with all the variables I could. In the end, the one that made the shader start working was to change the format on the RenderTexture that I was using to write `result` into:

    Code (CSharp):
    1. renderTexture.format = RenderTextureFormat.RFloat;
    2.  
    The default `RenderTextureFormat` doesn't work on Android (gives error `RENDERTEXTURE.CREATE FAILED: FORMAT UNSUPPORTED FOR RANDOM WRITES - RGBA4 UNORM (7)`). I had originally tried replacing it with `RenderTextureFormat.ARGB32` since that's the most common one. Turns out that doesn't work either. Maybe it needs a different declaration in the compute shader? I don't know - it all worked in the editor...
     
    wavekeyboard and Oxygeniium like this.