Search Unity

Question I don't know the difference between 'cg' and 'hlsl' in my custom shader (*noob...)

Discussion in 'Shaders' started by Actover, Sep 13, 2021.

  1. Actover

    Actover

    Joined:
    Jun 26, 2018
    Posts:
    4
    I would like to change the code below to be suitable for URP
    The purpose of this shader is to chromakey a specific color in '_mainTex'
    (Right now this shader is useful to me. It is used to erase blue-based colors.)


    Code (CSharp):
    1. Shader "Custom/MRSReal" {
    2.     Properties
    3.     {
    4.         _MainTex("Base (RGB) Trans (A)", 2D) = "" {}
    5.         _CKMap("Chroma-key Map", 2D) = "" {}
    6.     }
    7.  
    8.     CGINCLUDE
    9.     #include "UnityCG.cginc"
    10.     struct v2f
    11.     {
    12.         float4 pos : POSITION;
    13.         float2 uv : TEXCOORD0;
    14.     };
    15.  
    16.     sampler2D _MainTex;
    17.     sampler2D _CKMap;
    18.  
    19.     float _CKAlpha = float(1.0f);
    20.  
    21.     v2f vert(appdata_img v)
    22.     {
    23.         v2f o;
    24.         o.pos = UnityObjectToClipPos(v.vertex);
    25.         o.uv.xy = v.texcoord.xy;
    26.  
    27.         return o;
    28.     }
    29.  
    30.     float4 frag(v2f val) : COLOR
    31.     {
    32.         float4 yuv = tex2D(_MainTex, val.uv);
    33.         float4 ckmask = tex2D(_CKMap, float2(yuv.r, yuv.b));
    34.  
    35.         float a = 1.0f - ckmask.a;
    36.  
    37.  
    38.         float y = yuv.g;
    39.         float u = yuv.r - 0.5f;
    40.         float v = yuv.b - 0.5f;
    41.  
    42.         float r = y + (1.28033 * v);
    43.         float g = y + (-0.21482 * u) + (-0.38059 * v);
    44.         float b = y + (2.12798 * u);
    45.  
    46.         return float4(r, g, b, a);
    47.     }
    48.  
    49.     ENDCG
    50.     Subshader {
    51.         Tags{ "Queue" = "Transparent+1000" "IgnoreProjector" = "True" "RenderType" = "Transparent"  }
    52.         LOD 200
    53.         Alphatest Greater 0 ZWrite Off ColorMask RGB ZTest Always
    54.         Pass
    55.         {
    56.             Blend SrcAlpha OneMinusSrcAlpha
    57.             CGPROGRAM
    58.             #pragma vertex vert
    59.             #pragma fragment frag
    60.             #pragma target 3.0
    61.             ENDCG
    62.         }
    63.     }
    64.     Fallback off
    65. }



    Below is my first output. It compiles, but the results are different from shaders written in CG.
    The result is a greenish color, and none of the alpha values are properly applied.

    Code (CSharp):
    1. Shader "Custom/MRSReal_urp" {
    2.     Properties
    3.     {
    4.         _BaseMap("Base (RGB) Trans (A)", 2D) = "" {}
    5.         _CKMap("Chroma-key Map", 2D) = "" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags { "RenderType" = "Transparent" "RenderPipeline" = "UniversalRenderPipeline" }
    11.  
    12.         Pass
    13.         {
    14.             HLSLPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.  
    18.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"        
    19.  
    20.             struct Attributes
    21.             {
    22.                 float4 positionOS   : POSITION;
    23.                 float2 uv           : TEXCOORD0;
    24.             };
    25.  
    26.             struct Varyings
    27.             {
    28.                 float4 positionHCS  : SV_POSITION;
    29.                 float2 uv           : TEXCOORD0;
    30.             };
    31.  
    32.             TEXTURE2D(_BaseMap);
    33.             TEXTURE2D(_CKMap);
    34.             SAMPLER(sampler_BaseMap);
    35.             SAMPLER(sampler_CKMap);
    36.  
    37.             CBUFFER_START(UnityPerMaterial)
    38.             float4 _BaseMap_ST;
    39.             float4 _CKMap_ST;
    40.             CBUFFER_END
    41.  
    42.             Varyings vert(Attributes IN)
    43.             {
    44.                 Varyings OUT;
    45.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
    46.                 OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
    47.                 return OUT;
    48.             }
    49.  
    50.             half4 frag(Varyings IN) : SV_Target
    51.             {
    52.                 half4 yuv = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
    53.                 half4 ckmask = SAMPLE_TEXTURE2D(_CKMap, sampler_CKMap, float2(yuv.r, yuv.b));
    54.  
    55.                 float a = 1.0f - ckmask.a;
    56.  
    57.                 float y = yuv.g;
    58.                 float u = yuv.r - 0.5f;
    59.                 float v = yuv.b - 0.5f;
    60.  
    61.                 float r = y + (1.28033 * v);
    62.                 float g = y + (-0.21482 * u) + (-0.38059 * v);
    63.                 float b = y + (2.12798 * u);
    64.  
    65.                 return float4(r, g, b, a);
    66.             }
    67.             ENDHLSL
    68.         }
    69.     }
    70. }


    I struggled and forced myself to produce the following result. It worked, but again, the result was not the same as the CG. (My guess is that '_mainTex'(=_BaseMap) is the same as in CG, but _CKMap' is different in the operation process.)

    Code (CSharp):
    1. Shader "Custom/MRSReal_urp" {
    2.     Properties
    3.     {
    4.         _MainTex("Base (RGB) Trans (A)", 2D) = "" {}
    5.         _CKMap("Chroma-key Map", 2D) = "" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags { "Queue" = "Transparent+1000" "IgnoreProjector" = "True" "RenderType" = "Transparent" "RenderPipeline" = "UniversalRenderPipeline" }
    11.  
    12.         ColorMask RGB
    13.         ZTest Always
    14.         ZWrite Off
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.         Pass
    17.         {
    18.             HLSLPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #pragma target 3.0
    22.  
    23.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    24.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
    25.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
    26.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"      
    27.  
    28.             struct appdata_img
    29.             {
    30.                 float4 vertex   : POSITION;
    31.                 half2 texcoord  : TEXCOORD0;
    32.             };
    33.  
    34.             struct Varyings
    35.             {
    36.                 float4 pos  : POSITION;
    37.                 float2 uv   : TEXCOORD0;
    38.             };
    39.  
    40.             Varyings vert(appdata_img IN)
    41.             {
    42.                 Varyings OUT;
    43.                 OUT.pos = TransformObjectToHClip(IN.vertex.xyz);
    44.                 OUT.uv.xy = IN.texcoord.xy;
    45.                 return OUT;
    46.             }
    47.  
    48.             sampler2D _MainTex;
    49.             sampler2D _CKMap;
    50.             float _CKAlpha = float(1.0f);
    51.  
    52.             float4 frag(Varyings IN) : SV_TARGET
    53.             {
    54.                 float4 yuv = tex2D(_MainTex, IN.uv);
    55.                 float4 ckmask = tex2D(_CKMap, float2(yuv.r, yuv.b));
    56.  
    57.                 float a = 1.0f - ckmask.a;
    58.  
    59.                 float y = yuv.g;
    60.                 float u = yuv.r - 0.5f;
    61.                 float v = yuv.b - 0.5f;
    62.  
    63.                 float r = y + (1.28033 * v);
    64.                 float g = y + (-0.21482 * u) + (-0.38059 * v);
    65.                 float b = y + (2.12798 * u);
    66.  
    67.                 return float4(r, g, b, a);
    68.             }
    69.             ENDHLSL
    70.         }
    71.     }
    72.     Fallback off
    73. }


    Maybe there's a compilation order that I don't know. Please tell me how can I get the same result as CG... THx.... (please help X<)
     
    Last edited: Sep 13, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Here's the difference between
    CGPROGRAM
    and
    HLSLPROGRAM
    .

    CGPROGRAM
    automatically includes the UnityCG.cginc file.​

    That's it. They're both using HLSL. They both work in the built in renderer and the SRPs.

    CGPROGRAM
    comes from the fact Unity used to use a shader programming language called Cg, but they moved to pure HLSL many, many years ago. It was a gradual transition, and they didn't take the time to remove the remaining references to "cg" until the SRPs.

    There are differences between the built-in renderer and the SRPs in terms of what stuff is in the built-in UnityCG.cginc (and the files it includes) and those the SRP's many
    #include
    lines. The stuff in UnityCG.cginc may collide with the stuff in the SRP's .hlsl files as they cover a lot of the same ground (defining generic engine level transform variables and globals, etc.) so you only want to use either UnityCG.cginc or Core.hlsl but not both. And the functions for doing common things are named differently. For example the
    UnityObjectToClipPos()
    and
    TransformObjectToHClip()
    functions are named differently, but are under the hood effectively identical.

    Outside of that the SRPs have different requirements and ways it uses things like the tags. But none of that explains the difference you're seeing. The problem is I don't know what difference you're seeing as you haven't posted any screenshots of the before and after. But honestly my guess is it has nothing to do with the shader code at this point. I didn't notice anything obvious on a quick check over the first and last examples that would cause any problems.
     
    Darkgaze, tsunamigue, nan_nz and 2 others like this.