Frag Shader if else奇怪的行为

时间:2015-09-13 18:02:41

标签: unity3d shader fragment-shader vertex-shader

下面是我正在使用的当前着色器系统的简化版本,它将数据从一次性调度的计算着色器提取到顶点程序:v2f,然后将此数据解析为v2fc - 此结构存储静态添加了rend float的数据。 当前手动设置的浮点值将与外部输入,属性或可能是另一个计算内核链接。

正如在frag程序中看到的那样,这用于有效地决定是否呈现该实例。

我还尝试过使用' discard'功能,但我告诉这种方法是低效的。 另外,如果我尝试使用rend作为int,并且包括switch语句。

所有这些方法都给了我相同的结果:else或默认情况 无论switch / if条件如何,都始终执行选项。

我对c ++有一些经验,但主要是c#,对于复杂的着色器系统来说相对较新,所以如果语法不正确,请原谅我。

我感谢对此问题的任何建议或解释。

//TODO Rendfiltermethods

Shader "Custom/StarShaderTemp" 
{

Properties 
{
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Color(" Color", Color) = (1, 0.5, 0.5, 1)
    _Scale(" Scale ", Float) = 1
    _Damping(" Damping Value", Float) = 500

}

SubShader 
{
    Pass 
    {
        Tags { "RenderType" = "Transparent"}
        ZTest Off
        ZWrite Off
        Cull Off
        Blend SrcAlpha One

        CGPROGRAM
        #pragma target 5.0

        #pragma vertex star_vertex
        #pragma vertex star_cull
        #pragma fragment frag
        #pragma exclude_renderers gles
        #include "UnityCG.cginc"
        #include "StarStructInc.cginc"

             StructuredBuffer<Star> stars;
             StructuredBuffer<float3> quadPoints; 

             StructuredBuffer<int> cullint; //Input from cull compute shader for bypassing gifmain compute

            sampler2D _MainTex;

            sampler2D _O;
            sampler2D _B;
            sampler2D _A;
            sampler2D _F;
            sampler2D _G;
            sampler2D _K;
            sampler2D _M;
            sampler2D _Blackhole;

            float4 _Color;
            float _Scale;
            float _CamDist;
            float _Damping;

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv  : TEXCOORD0;
                float4 color : COLOR;

            };

            struct v2fc
            {
             v2f vetwof : v2f;
             float rendfloat: float;    
            };


            // vertex shader with no inputs
            // uses the system values SV_VertexID and SV_InstanceID to read from compute buffers
            v2f star_vertex(uint id : SV_VertexID, uint inst : SV_InstanceID)
            {
                v2f o;

                float3 worldPosition = stars[inst].position;
                float3 Qpoint = quadPoints[id] * _Scale * sqrt(_CamDist / _Damping);

                o.pos = mul (UNITY_MATRIX_P, mul (UNITY_MATRIX_V, float4(worldPosition, 1.0f)) + float4(Qpoint, 0.0f));

                o.uv = quadPoints[id] + 0.5f; //setting UV texture coord center 

                return o;
            }

            v2fc star_cull(v2f i)
            {
                v2fc c;
                c.vetwof = i;
                c.rendfloat = 6.0;              
                return c;
            }


            float4 frag (v2fc i) : COLOR
            {                   
                    if(i.rendfloat < 5.0)
                    {
                    float4 texColn = tex2D (_MainTex, i.vetwof.uv);
                    return texColn * _Color;    
                    }
                    else
                    {   
                     i.vetwof.pos =0;       
                    float4 texColn = tex2D (_MainTex, i.vetwof.uv);
                    return texColn * _Color;                                    

                    }               

            }               
        ENDCG
    }
} 

FallBack "Diffuse"

}

1 个答案:

答案 0 :(得分:0)

通过创建void函数而不是成功frag ETC来纠正else行为。在查看编译的代码之后,似乎在优化组装时使用的分支预测存在问题,因为可以组合frag函数引用。 另一个修复是使用渲染目标的多次传递。