Search Unity

exp() doesn't work in shader model 2.0

Discussion in 'Shaders' started by Lex-DRL, Jun 9, 2014.

  1. Lex-DRL

    Lex-DRL

    Joined:
    Oct 10, 2011
    Posts:
    140
    I'm trying to implement Unity's Fog behavior in my own CG code.

    Everything was fine until I used HLSL's exp() function. For some reason, it doesn't allow the shader to be compiled for shader model 2.0 (the default one, pragma target 2.0). Though exp() specification itself tells that this function is supported not only in 2.0, but even in 1.1.

    Despite that, my shader gives warning "No subshaders can run on this graphics card" and doesn't work.

    However, if I simply comment out the line containing exp() function, the shader suddenly compiles as it shoud.

    Why doesn't it work? What do I need to do to make it work?

    Unity 4.3.4, here's my test shader:
    Code (CSharp):
    1. Shader "DRL/testFog-My" {
    2. Properties {
    3.     _MainTex ("Base (RGB)", 2D) = "black" {}
    4. }
    5.  
    6. SubShader {
    7.     Tags { "RenderType" = "Opaque" }
    8.     Pass {
    9.         // ZWrite off
    10.         Cull off
    11.         Lighting Off
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.         Fog {Mode Off}
    14.        
    15.         CGPROGRAM
    16.         #pragma target 2.0
    17.         #pragma vertex vert
    18.         #pragma fragment frag
    19.         //#pragma exclude_renderers d3d11 xbox360 ps3 flash d3d11_9x
    20.        
    21.         #include "UnityCG.cginc"
    22.        
    23.         uniform half4 unity_FogColor;
    24.         uniform half unity_FogDensity;
    25.         sampler2D _MainTex;
    26.        
    27.         struct vertexInput {
    28.             float4 vertex : POSITION;
    29.             float2 texcoord : TEXCOORD0;
    30.         };
    31.        
    32.         struct v2f {
    33.             float4 scrPos : SV_POSITION;
    34.             half2 srcUVs: TEXCOORD0;
    35.             half2 fogDepth: TEXCOORD1; // linear depth in x and depth multiplied by density in y
    36.         };
    37.        
    38.        
    39.        
    40.         v2f vert (vertexInput v)
    41.         {
    42.             v2f o;
    43.             o.scrPos = mul(UNITY_MATRIX_MVP, v.vertex);
    44.             o.srcUVs = v.texcoord;
    45.            
    46.             o.fogDepth.x = length(mul (UNITY_MATRIX_MV, v.vertex).xyz);
    47.             o.fogDepth.y = o.fogDepth.x * unity_FogDensity;
    48.            
    49.             return o;
    50.         }
    51.        
    52.         fixed4 frag (v2f i) : COLOR
    53.         {
    54.             //fixed3 clr = tex2D(_MainTex, i.srcUVs).rgb;
    55.            
    56.             // Exp2 mode:
    57.             float fogAmt = i.fogDepth.y * i.fogDepth.y;
    58.             fogAmt = exp(-fogAmt);
    59.            
    60.             return fixed4(
    61.                 //clr,
    62.                 fogAmt,
    63.                 fogAmt,
    64.                 fogAmt,
    65.                 1.0f
    66.             );
    67.         }
    68.         ENDCG
    69.     }
    70. }
    71.  
    72. }