Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Resolved Grass Compute shader appearing pink in mobile.

Discussion in 'General Graphics' started by codevisionary005, Aug 28, 2022.

  1. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
    I have been trying out a grass compute shader by minionsart from patreon and it works awesome in pc but even though my mobile supports compute shaders, The grass appears pink on a sphere with no wind sway while it should have been all over a plane moving with the wind. if the shader code is important in analyzing this, please tell me.
    Here are some images..
     

    Attached Files:

  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,022
    Did you try in windows standalone build?
    If not there put the shader in always included shaders
     
    codevisionary005 likes this.
  3. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,284
    Mobile often can’t handle as many thread groups etc as desktop. My guess is you exceeded a limit. SystenInfo has methods to check all the limits for the platform.
     
    codevisionary005 likes this.
  4. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
    Yes but the shader works fine when put in unity remote in my mobile but when i build the games apk, this happens.
     
  5. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
    It works fine in the windows standalone build.
     
  6. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
    and by the way, I use URP (LWRP)
     
  7. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,022
    URP or LWRP?
    And what unity version? Tried the latest lts already?

    Oh wait, do you use vulkan? openGL doesn't support compute shaders well
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,102
    Just in case you're not aware Unity Remote doesn't actually run your application on your mobile device. Instead it's running your application on your PC and simply passing the rendered output of the PC to the mobile device and the inputs of the mobile device back to the PC.
     
  9. georgerh

    georgerh

    Joined:
    Feb 28, 2020
    Posts:
    72
  10. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,011
    You should check the device logs - there should be a message that details what didn't work out.
     
  11. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
    I use URP unity 2021.3.4f1 and after you posted this, i found that vulcan wasn't added to my graphics api list in project settings so when I tried to put it on, unity crashed.
    Oh.. I didn't know that since i'm a beginner to developing to mobile.
    I will try that.
    Yes you are right... I found the device log via command prompt and this is the error
     GLSL link error:  The number of vertex shader storage blocks (1) is greater than the maximum number allowed (0).
    How do I fix this?
     
  12. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,011
  13. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
    The error above is from one of the grass shader files. Here is the code.
    Code (CSharp):
    1. Shader "Custom/GrassComputeHLSL"
    2. {
    3.     Properties
    4.     {
    5.         [Toggle(BLEND)] _BlendFloor("Blend with floor", Float) = 0
    6.         _Fade("Top Fade Offset", Range(-1,10)) = 0
    7.         _AmbientAdjustment("Ambient Adjustment", Range(-1,10)) = 0
    8.     }
    9.    
    10.     HLSLINCLUDE
    11.     // Include some helper functions
    12.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    13.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    14.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
    15.    
    16.     // This describes a vertex on the generated mesh
    17.     struct DrawVertex
    18.     {
    19.         float3 positionWS; // The position in world space
    20.         float2 uv;
    21.         float3 diffuseColor;
    22.     };
    23.    
    24.     // A triangle on the generated mesh
    25.     struct DrawTriangle
    26.     {
    27.         float3 normalOS;
    28.         DrawVertex vertices[3]; // The three points on the triangle
    29.     };
    30.    
    31.     // A buffer containing the generated mesh
    32.     StructuredBuffer<DrawTriangle> _DrawTriangles;
    33.    
    34.     struct v2f
    35.     {
    36.         float4 positionCS : SV_POSITION; // Position in clip space
    37.         float2 uv : TEXCOORD0;          // The height of this vertex on the grass blade
    38.         float3 positionWS : TEXCOORD1; // Position in world space
    39.         float3 normalWS : TEXCOORD2;   // Normal vector in world space
    40.         float3 diffuseColor : COLOR;
    41.         float fogFactor : TEXCOORD5;
    42.     };
    43.    
    44.     float4 _TopTint;
    45.     float4 _BottomTint;
    46.     float _Fade;
    47.     float4 _PositionMoving;
    48.     float _OrthographicCamSize;
    49.     float3 _OrthographicCamPos;
    50.     uniform sampler2D _TerrainDiffuse;
    51.     float _AmbientAdjustment;
    52.     // ----------------------------------------
    53.    
    54.     // Vertex function
    55.    
    56.     // -- retrieve data generated from compute shader
    57.     v2f vert(uint vertexID : SV_VertexID)
    58.     {
    59.         // Initialize the output struct
    60.         v2f output = (v2f)0;
    61.        
    62.         // Get the vertex from the buffer
    63.         // Since the buffer is structured in triangles, we need to divide the vertexID by three
    64.         // to get the triangle, and then modulo by 3 to get the vertex on the triangle
    65.         DrawTriangle tri = _DrawTriangles[vertexID / 3];
    66.         DrawVertex input = tri.vertices[vertexID % 3];
    67.        
    68.         output.positionCS = TransformWorldToHClip(input.positionWS);
    69.         output.positionWS = input.positionWS;
    70.        
    71.         float3 faceNormal = GetMainLight().direction * tri.normalOS;
    72.         output.normalWS = TransformObjectToWorldNormal(faceNormal, true);
    73.         float fogFactor = ComputeFogFactor(output.positionCS.z);
    74.         output.fogFactor = fogFactor;
    75.         output.uv = input.uv;
    76.        
    77.         output.diffuseColor = input.diffuseColor;
    78.        
    79.         return output;
    80.     }
    81.    
    82.     // ----------------------------------------
    83.    
    84.     // Fragment function
    85.    
    86.     half4 frag(v2f i) : SV_Target
    87.     {
    88.         // For Shadow Caster Pass
    89.         #ifdef SHADERPASS_SHADOWCASTER
    90.             return 0;
    91.         #else
    92.             // For Color Pass
    93.             // rendertexture UV for terrain blending
    94.             float2 uv = i.positionWS.xz - _OrthographicCamPos.xz;
    95.             uv = uv / (_OrthographicCamSize * 2);
    96.             uv += 0.5;
    97.            
    98.             // get ambient color from environment lighting
    99.             float4 ambient =float4(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w,1); //float4(ShadeSH9(float4(0,0,1,1)),0);
    100.            
    101.             float shadow = 0;
    102.             #if BLEND
    103.                 shadow = 1;              
    104.             #endif
    105.            
    106.            
    107.             half4 shadowCoord = TransformWorldToShadowCoord(i.positionWS);
    108.            
    109.             #if _MAIN_LIGHT_SHADOWS_CASCADE || _MAIN_LIGHT_SHADOWS
    110.                 Light mainLight = GetMainLight(shadowCoord);
    111.                 shadow = mainLight.shadowAttenuation;
    112.             #else
    113.                 Light mainLight = GetMainLight();
    114.             #endif
    115.            
    116.             // extra point lights support
    117.             float3 extraLights;
    118.             int pixelLightCount = GetAdditionalLightsCount();
    119.             for (int j = 0; j < pixelLightCount; ++j) {
    120.                 Light light = GetAdditionalLight(j, i.positionWS, half4(1, 1, 1, 1));
    121.                 float3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
    122.                 extraLights += attenuatedLightColor;
    123.                
    124.             }
    125.             // fade over the length of the grass
    126.             float verticalFade = saturate(i.uv.y + _Fade);
    127.             extraLights *= verticalFade;
    128.             // colors from the tool with tinting from the grass script
    129.             float4 baseColor = lerp(_BottomTint , _TopTint,verticalFade) * float4(i.diffuseColor, 1);
    130.             // get the floor map
    131.             float4 terrainForBlending = tex2D(_TerrainDiffuse, uv);
    132.            
    133.             float4 final = float4(0,0,0,0);
    134.             #if BLEND    
    135.                 _TopTint = _TopTint * ambient;
    136.                 // tint the top blades and add in light color            
    137.                 terrainForBlending = lerp(terrainForBlending,terrainForBlending+ ( _TopTint* float4(i.diffuseColor, 1)) , verticalFade);
    138.                 final = lerp((terrainForBlending)  * shadow , terrainForBlending, shadow);
    139.                 // add in ambient and attempt to blend in with the shadows
    140.                 final += lerp((ambient * terrainForBlending) * _AmbientAdjustment, 0,shadow);
    141.             #else
    142.                 final = baseColor;
    143.                 // add in shadows
    144.                 final *= shadow;
    145.                 // if theres a main light, multiply with its color and intensity          
    146.                 final *= float4(mainLight.color,1);        
    147.                
    148.                 // add in ambient
    149.                 final += (ambient * baseColor) ;
    150.             #endif
    151.             final += float4(extraLights,1);
    152.             // fog
    153.             float fogFactor = i.fogFactor;
    154.             // Mix the pixel color with fogColor.
    155.             final.rgb = MixFog(final.rgb, fogFactor);        
    156.             return final;
    157.            
    158.         #endif  // SHADERPASS_SHADOWCASTER
    159.     }
    160.     ENDHLSL
    161.    
    162.     SubShader {
    163.         // UniversalPipeline needed to have this render in URP
    164.         Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" }
    165.        
    166.         // Forward Lit Pass
    167.         Pass
    168.         {
    169.             Name "ForwardLit"
    170.             Tags { "LightMode" = "UniversalForward" }
    171.             Cull Off // No culling since the grass must be double sided
    172.            
    173.             HLSLPROGRAM
    174.             // Signal this shader requires a compute buffer
    175.             #pragma prefer_hlslcc gles
    176.             #pragma exclude_renderers d3d11_9x
    177.             #pragma target 5.0
    178.            
    179.             // Lighting and shadow keywords
    180.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    181.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    182.             #pragma multi_compile _ _ADDITIONAL_LIGHTS
    183.             #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
    184.             #pragma multi_compile _ _SHADOWS_SOFT
    185.             #pragma multi_compile_fog
    186.             #pragma shader_feature BLEND
    187.             // Register our functions
    188.             #pragma vertex vert
    189.             #pragma fragment frag
    190.            
    191.             ENDHLSL
    192.         }
    193.        
    194.         // Shadow Casting Pass
    195.         Pass
    196.         {
    197.             Name "ShadowCaster"
    198.             Tags { "LightMode" = "ShadowCaster" }
    199.             ZWrite On
    200.             ZTest LEqual
    201.             Cull Off
    202.            
    203.             HLSLPROGRAM
    204.             // Signal this shader requires geometry function support
    205.             #pragma prefer_hlslcc gles
    206.             #pragma exclude_renderers d3d11_9x
    207.             #pragma target 5.0
    208.            
    209.             // Support all the various light  ypes and shadow paths
    210.             #pragma multi_compile_shadowcaster
    211.            
    212.             // Register our functions
    213.             #pragma vertex vert
    214.             #pragma fragment frag
    215.            
    216.             // A custom keyword to modify logic during the shadow caster pass
    217.             #define SHADERPASS_SHADOWCASTER
    218.            
    219.             #pragma shader_feature_local _ DISTANCE_DETAIL
    220.            
    221.             ENDHLSL
    222.         }
    223.     }
    224. }
     
  14. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
  15. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,022
    Using vulkan under android might fix this right?

    @codevisionary005 did you add vulkan under android or desktop? You can use it for android and use automatic for desktop
     
  16. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,011
    @DevDunk Right, it should work on Vulkan if the ComputeBuffer is read-only.
     
    DevDunk likes this.
  17. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    One alternative is to use a RenderTexture instead of a ComputeBuffer. They are less flexible to work with, but reading from textures on vertex shaders should be supported on ES 3.0 and up.
     
    richardkettlewell and DevDunk like this.
  18. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
    I tried to, but wheneven I add vulcan, Unity crashes.
    The ComputeBuffer is read-only but as I said earlier, I couldn't add vulcan since unity crashes.
    Do you have any resources for RenderTexture? Because i'm new to shader programming and i'm do not know if it could help me generate grass blades as the computebuffer does.
     
  19. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,022
    But unity shouldn't crash if you change the android API. Unity itself should still run in dx11 in that case. Do make a bug report (after trying the latest LTS of your unity version) for it since it shouldn't happen
     
  20. codevisionary005

    codevisionary005

    Joined:
    Nov 15, 2021
    Posts:
    17
    Good news. After @DevDunk told me to try the latest LTS of my unity version, I tried it. It didn't work, but then I tried 2021.3.0f1 which is a version older than what I currently use, The shader now works!:D Thank you for all your help, I will be changing the thread's tag to resolved.
     
    DevDunk likes this.
  21. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,022
    Could you make a bug report on the latest lts in that case?
    To get the issue fixed