Search Unity

Resolved shadergraph performance worries: it generates shader with branches?

Discussion in 'Shader Graph' started by laurentlavigne, Oct 6, 2020.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,316
    aren't branches slow on gpu?
    surprised to find this jewel at the end of the fragment

    return all(isfinite(surf.Diffuse_7)) ? half4(surf.Diffuse_7.x, surf.Diffuse_7.y, surf.Diffuse_7.z, 1.0) : float4(1.0f, 0.0f, 1.0f, 1.0f);


    Code (CSharp):
    1. Shader "Custom Function"
    2. {
    3.     Properties
    4.     {
    5.         [NoScaleOffset]_BaseMap("BaseMap", 2D) = "white" {}
    6.         [NoScaleOffset]_NormalMap("NormalMap", 2D) = "white" {}
    7.         [HDR]_SpecColor("SpecColor", Color) = (1.135301, 1.135301, 1.135301, 0)
    8.         _Smoothness("Smoothness", Float) = 0.5
    9.         [HDR]Color_F5CA356F("SSS", Color) = (0.7264151, 0.3387442, 0.1473389, 0)
    10.     }
    11.  
    12.     HLSLINCLUDE
    13.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    14.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"
    15.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/NormalSurfaceGradient.hlsl"
    16.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    17.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
    18.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
    19.     #include "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl"
    20.     #include "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl"
    21.     #include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl"
    22.     #define SHADERGRAPH_PREVIEW 1
    23.  
    24.     CBUFFER_START(UnityPerMaterial)
    25.     half4 _SpecColor;
    26.     half _Smoothness;
    27.     half4 Color_F5CA356F;
    28.     CBUFFER_END
    29.     TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap); half4 _BaseMap_TexelSize;
    30.     TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap); half4 _NormalMap_TexelSize;
    31.  
    32.     struct SurfaceDescriptionInputs
    33.     {
    34.         float3 WorldSpaceNormal;
    35.         float3 WorldSpaceViewDirection;
    36.         float3 WorldSpacePosition;
    37.     };
    38.  
    39.  
    40.     // ddb8951f46b5bf7aa9aa154e10897ab5
    41.     #include "Assets/art/shaders/CustomLighting.hlsl"
    42.  
    43.     struct SurfaceDescription
    44.     {
    45.         half3 Diffuse_7;
    46.     };
    47.  
    48.     SurfaceDescription PopulateSurfaceData(SurfaceDescriptionInputs IN)
    49.     {
    50.         SurfaceDescription surface = (SurfaceDescription)0;
    51.         half3 _CustomFunction_69DE6333_Diffuse_7;
    52.         half3 _CustomFunction_69DE6333_Specular_8;
    53.         AdditionalLights_half(half3 (0, 0, 0), 0, IN.WorldSpacePosition, IN.WorldSpaceNormal, IN.WorldSpaceViewDirection, _CustomFunction_69DE6333_Diffuse_7, _CustomFunction_69DE6333_Specular_8);
    54.         surface.Diffuse_7 = _CustomFunction_69DE6333_Diffuse_7;
    55.         return surface;
    56.     }
    57.  
    58.     struct GraphVertexInput
    59.     {
    60.         float4 vertex : POSITION;
    61.         float3 normal : NORMAL;
    62.         UNITY_VERTEX_INPUT_INSTANCE_ID
    63.     };
    64.  
    65.     GraphVertexInput PopulateVertexData(GraphVertexInput v)
    66.     {
    67.         return v;
    68.     }
    69.  
    70.     ENDHLSL
    71.  
    72.     SubShader
    73.     {
    74.         Tags { "RenderType"="Opaque" }
    75.         LOD 100
    76.  
    77.         Pass
    78.         {
    79.             HLSLPROGRAM
    80.             #pragma vertex vert
    81.             #pragma fragment frag
    82.  
    83.             struct GraphVertexOutput
    84.             {
    85.                 float4 position : POSITION;
    86.                 float3 WorldSpacePosition : TEXCOORD0;
    87.             float3 WorldSpaceNormal : TEXCOORD1;
    88.             float3 WorldSpaceViewDirection : TEXCOORD2;
    89.  
    90.             };
    91.  
    92.             GraphVertexOutput vert (GraphVertexInput v)
    93.             {
    94.                 v = PopulateVertexData(v);
    95.  
    96.                 GraphVertexOutput o;
    97.                 float3 positionWS = TransformObjectToWorld(v.vertex);
    98.                 o.position = TransformWorldToHClip(positionWS);
    99.                 float3 WorldSpacePosition = mul(UNITY_MATRIX_M,v.vertex).xyz;
    100.             float3 WorldSpaceNormal = normalize(mul(v.normal,(float3x3)UNITY_MATRIX_I_M));
    101.             float3 WorldSpaceViewDirection = _WorldSpaceCameraPos.xyz - mul(GetObjectToWorldMatrix(), float4(v.vertex.xyz, 1.0)).xyz;
    102.                 o.WorldSpacePosition = WorldSpacePosition;
    103.                 o.WorldSpaceNormal = WorldSpaceNormal;
    104.                 o.WorldSpaceViewDirection = WorldSpaceViewDirection;
    105.  
    106.                 return o;
    107.             }
    108.  
    109.             float4 frag (GraphVertexOutput IN ) : SV_Target
    110.             {
    111.                 float3 WorldSpacePosition = IN.WorldSpacePosition;
    112.             float3 WorldSpaceNormal = IN.WorldSpaceNormal;
    113.             float3 WorldSpaceViewDirection = IN.WorldSpaceViewDirection;
    114.  
    115.                 SurfaceDescriptionInputs surfaceInput = (SurfaceDescriptionInputs)0;
    116.                 surfaceInput.WorldSpaceNormal = WorldSpaceNormal;
    117.             surfaceInput.WorldSpaceViewDirection = WorldSpaceViewDirection;
    118.             surfaceInput.WorldSpacePosition = WorldSpacePosition;
    119.  
    120.                 SurfaceDescription surf = PopulateSurfaceData(surfaceInput);
    121.                 return all(isfinite(surf.Diffuse_7)) ? half4(surf.Diffuse_7.x, surf.Diffuse_7.y, surf.Diffuse_7.z, 1.0) : float4(1.0f, 0.0f, 1.0f, 1.0f);
    122.  
    123.             }
    124.             ENDHLSL
    125.         }
    126.     }
    127. }
    128.  
    Code (CSharp):
    1. void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
    2. {
    3.     #if SHADERGRAPH_PREVIEW
    4.     Direction = half3(0.5, 0.5, 0);
    5.     Color = 1;
    6.     DistanceAtten = 1;
    7.     ShadowAtten = 1;
    8.     #else
    9.     #if SHADOWS_SCREEN
    10.     half4 clipPos = TransformWorldToHClip(WorldPos);
    11.     half4 shadowCoord = ComputeScreenPos(clipPos);
    12.     #else
    13.     half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    14.     #endif
    15.     Light mainLight = GetMainLight(shadowCoord);
    16.     Direction = mainLight.direction;
    17.     Color = mainLight.color;
    18.     DistanceAtten = mainLight.distanceAttenuation;
    19.     ShadowAtten = mainLight.shadowAttenuation;
    20.     #endif
    21. }
    22.  
    23. void DirectSpecular_half(half3 Specular, half Smoothness, half3 Direction, half3 Color, half3 WorldNormal, half3 WorldView, out half3 Out)
    24. {
    25.     #if SHADERGRAPH_PREVIEW
    26.     Out = 0;
    27.     #else
    28.     Smoothness = exp2(10 * Smoothness + 1);
    29.     WorldNormal = normalize(WorldNormal);
    30.     WorldView = SafeNormalize(WorldView);
    31.     Out = LightingSpecular(Color, Direction, WorldNormal, WorldView, half4(Specular, 0), Smoothness);
    32.     #endif
    33. }
    34.  
    35. void AdditionalLights_half(half3 SpecColor, half Smoothness, half3 WorldPosition, half3 WorldNormal, half3 WorldView, out half3 Diffuse, out half3 Specular)
    36. {
    37.     half3 diffuseColor = 0;
    38.     half3 specularColor = 0;
    39.  
    40.     #ifndef SHADERGRAPH_PREVIEW
    41.     Smoothness = exp2(10 * Smoothness + 1);
    42.     WorldNormal = normalize(WorldNormal);
    43.     WorldView = SafeNormalize(WorldView);
    44.     int pixelLightCount = GetAdditionalLightsCount();
    45.     for (int i = 0; i < pixelLightCount; ++i)
    46.     {
    47.         Light light = GetAdditionalLight(i, WorldPosition);
    48.         half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
    49.         diffuseColor += LightingLambert(attenuatedLightColor, light.direction, WorldNormal);
    50.         specularColor += LightingSpecular(attenuatedLightColor, light.direction, WorldNormal, WorldView, half4(SpecColor, 0), Smoothness);
    51.     }
    52.     #endif
    53.  
    54.     Diffuse = diffuseColor;
    55.     Specular = specularColor;
    56. }
     

    Attached Files:

  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    This isn't a branch, it's a conditional move.
     
    laurentlavigne likes this.
  3. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,316
    gotcha, thanks