CG 着色器适用于 DirectX 但不适用于 Opengl

CG shader works on DirectX but not Opengl

我正在尝试编写一个支持 float NaN 值的简单模糊片段着色器,但它在 OpenGL 上给我带来了意想不到的结果。我使用浮点纹理,如果像素为 NaN,则将绿色通道设置为 1,否则将红色通道设置为浮点值。

在 DirectX 上,它工作正常,但在 OpenGL 上它 returns 黑色而不是我期望的 NaN 值的绿色。我用 GTX 1060 在 Ubuntu 上测试,用 GTX 750

在 Windows 10 上测试

DirectX 输出,预期结果:

OpenGL 输出,不正确的结果:

CG代码:

Shader "FX/DepthBlur_R"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            float4 _MainTex_TexelSize;

            float4 frag (v2f i) : SV_Target
            {
                float2 col = tex2D(_MainTex, i.uv).rg;
                float2 up = tex2D(_MainTex, i.uv + fixed2(0, _MainTex_TexelSize.y)).rg;
                float2 down = tex2D(_MainTex, i.uv - fixed2(0, _MainTex_TexelSize.y)).rg;
                float2 left = tex2D(_MainTex, i.uv - fixed2(_MainTex_TexelSize.x, 0)).rg;
                float2 right = tex2D(_MainTex, i.uv + fixed2(_MainTex_TexelSize.x, 0)).rg;

                int count = 0;
                float sides = 0;
                if (up.g < 0.1) { sides += up.r; count++; }
                if (down.g < 0.1) { sides += down.r; count++; }
                if (left.g < 0.1) { sides += left.r; count++; }
                if (right.g < 0.1) { sides += right.r; count++; }
                sides = sides / count;

                float4 ret;
                if (count == 0 && col.g > 0.1) { // Nothing valid
                    ret = fixed4(0, 1, 0, 1);
                }
                else if (count == 0) { // Only col is valid
                    ret = float4(col.r, 0, 0, 1);
                }
                else if (col.g > 0.1) { // Only sides are valid
                    ret = float4(sides, 0, 0, 1);
                }
                else {
                    ret = float4(((col.r + sides) / 2), 0, 0, 1);
                }
                return ret;
            }
            ENDCG
        }
    }
}

切换到debug colors的结果也很奇怪,看else部分的评论:

if (count == 0 && col.g > 0.1) { // Nothing valid
    return fixed4(1, 0, 0, 1);
}
else if (count == 0) { // Only col is valid
    return fixed4(0, 1, 0, 1);
}
else if (col.g > 0.1) { // Only sides are valid
    return fixed4(0, 0, 1, 1);
}
else {
    //Uncommenting this line gives the expected result
    return fixed4(0, 0, 0, 1);
    //Uncommenting this line results in blue for the "Nothing valid" section
    //return float4(0, 0, 0, ((col.r + sides) / 2));
}

不知何故将 count 从 int 转换为 float 解决了这个问题。有谁知道是什么原因导致的?