从DX9到DX11的顶点着色器编译错误(Unity 5.6到2017.4)

时间:2018-04-16 14:15:34

标签: unity3d graphics shader hlsl

我使用自定义着色器生成Unity图像后期效果,从Unity 5.6迁移到Unity 2017.4时会抛出错误

  

断言失败:无法创建DX11顶点声明;某物   顶点着色器输入数据错误? (HR = 80070057)

这个剧本似乎是个错误。

Shader "SG/Stretch Shadows" {
Properties {
    _MainTex ("Light texture", 2D) = "white" {}
}
SubShader {
    LOD 100
    Blend One Zero
    Cull Off
    ZWrite Off
    Lighting Off

    Pass {    // 0 Blacken source
        CGPROGRAM
            #pragma target 3.0

            #include "UnityCG.cginc"
            #pragma vertex processVerts
            #pragma fragment drawFrag

            #pragma glsl_no_auto_normalization

            struct vertdata {
                float2 uv : TEXCOORD0;
                float2 uvScaled : TEXCOORD1;
                float4 vertex : POSITION;
            };

            struct frag_v2f {
                float4 vertex : SV_POSITION;
                half2 uv : TEXCOORD0;
            };

            uniform sampler2D _MainTex;

            frag_v2f processVerts (vertdata v){
                frag_v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                return o;
            }

            half4 drawFrag (frag_v2f i) : SV_Target {
                fixed4 shadow = tex2D(_MainTex, i.uv);
                half intensity = max(shadow.r, max(shadow.g, shadow.b));
                intensity = step(intensity,0.1);
                return 1-intensity;
            }

        ENDCG
    }

    Pass {    // 1 Stretch
        CGPROGRAM
            #pragma target 3.0

            #include "UnityCG.cginc"
            #pragma vertex processVerts
            #pragma fragment drawFrag

            #pragma glsl_no_auto_normalization

            struct vertdata {
                float2 uv : TEXCOORD0;
                float2 uvScaled : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            struct frag_v2f {
                float4 vertex : SV_POSITION;
                half2 basePos : TEXCOORD0;
                half2 zoomPos : TEXCOORD1;
            };

            uniform sampler2D _ObstacleTex;
            uniform sampler2D _MainTex;
            half4 _SunPos;
            #ifdef UNITY_HALF_TEXEL_OFFSET
            #endif

            frag_v2f processVerts (vertdata v){
                frag_v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                 o.basePos = v.uv;
                o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
                return o;
            }

            half4 drawFrag (frag_v2f i) : SV_Target {
                half2 basePos = i.basePos;
                half2 zoomPos = i.zoomPos;
                half4 tex = tex2D(_MainTex, i.basePos);

                half sub = 1.0/60;
                half len = length((basePos - zoomPos));

                half4 col = tex*tex.a;
                col = half4(0,0,0,0);
                half pos = 1;
                half power = 0.5;

                for(int i = 0; i < 60; i++){
                    pos -= sub;
                    half4 obstacle = tex2D(_MainTex, lerp(zoomPos, basePos, pos));
                    obstacle *= pow(pos, power);
                    col = max(col, obstacle);
                }

                return 1-col;//half4(half3((basePos - zoomPos).x*20), 1);//tex2D(_ObstacleTex, basePos);
            }

        ENDCG
    }

    Pass {    // 2 Blacken alpha
        CGPROGRAM
            #pragma target 3.0

            #include "UnityCG.cginc"
            #pragma vertex processVerts
            #pragma fragment drawFrag

            #pragma glsl_no_auto_normalization

            struct vertdata {
                float2 uv : TEXCOORD0;
                float2 uvScaled : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            struct frag_v2f {
                float4 vertex : SV_POSITION;
                half2 uv : TEXCOORD0;
            };

            uniform sampler2D _MainTex;

            frag_v2f processVerts (vertdata v){
                frag_v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                return o;
            }

            half4 drawFrag (frag_v2f i) : SV_Target {
                fixed4 shadow = tex2D(_MainTex, i.uv);
                half4 intensity = half4(0,0.2,0.2,shadow.a);
                intensity = lerp(shadow, intensity, shadow.a);

                return intensity;
            }

        ENDCG
    }

    Pass {    // 3 Draw fully zoomed shadow and half zoomed shadow
        // Each pass duplicates previous pass, doubling drawn shadows
        CGPROGRAM
            #pragma target 3.0

            #include "UnityCG.cginc"
            #pragma vertex processVerts
            #pragma fragment drawFrag

            #pragma glsl_no_auto_normalization

            struct vertdata {
                float2 uv : TEXCOORD0;
                float2 uvScaled : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            struct frag_v2f {
                float4 vertex : SV_POSITION;
                half2 basePos : TEXCOORD0;
                half2 zoomPos : TEXCOORD1;
            };

            uniform sampler2D _ObstacleTex;
            uniform sampler2D _MainTex;
            half4 _SunPos;
            half _Offset;                                            // frational lerp value. Decrease by powers of two each pass
            #ifdef UNITY_HALF_TEXEL_OFFSET
            #endif

            frag_v2f processVerts (vertdata v){
                frag_v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                 o.basePos = v.uv;
                o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
                return o;
            }

            half4 drawFrag (frag_v2f i) : SV_Target {
                half2 basePos = i.basePos;
                half2 zoomPos = i.zoomPos;
                half firstPass = tex2D(_MainTex, basePos).a;
                half secondPass = tex2D(_MainTex, zoomPos).a-0.85;
                half4 output = half4(0,0,0,max(firstPass, secondPass));
                secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.65)).a-0.3;
                output.a = max(output.a, secondPass);
                secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.4)).a-0.45;
                output.a = max(output.a, secondPass);
                secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.25)).a-0.6;
                output.a = max(output.a, secondPass);
                secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.1)).a-0.7;
                output.a = max(output.a, secondPass);

                return output;//half4(half3((basePos - zoomPos).x*20), 1);//tex2D(_ObstacleTex, basePos);
            }

        ENDCG
    }

Pass {    // 4 Single pass. Ping-pong
        // Each pass duplicates previous pass, doubling drawn shadows
        CGPROGRAM
            #pragma target 3.0

            #include "UnityCG.cginc"
            #pragma vertex processVerts
            #pragma fragment drawFrag

            #pragma glsl_no_auto_normalization

            struct vertdata {
                float2 uv : TEXCOORD0;
                float2 uvScaled : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            struct frag_v2f {
                float4 vertex : SV_POSITION;
                half2 basePos : TEXCOORD0;
                half2 zoomPos : TEXCOORD1;
            };

            uniform sampler2D _ObstacleTex;
            uniform sampler2D _MainTex;
            half4 _SunPos;
            half _Offset;                                            // frational lerp value. Decrease by powers of two each pass
            #ifdef UNITY_HALF_TEXEL_OFFSET
            #endif

            frag_v2f processVerts (vertdata v){
                frag_v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                 o.basePos = v.uv;
                o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
                return o;
            }

            half4 drawFrag (frag_v2f i) : SV_Target {
                half firstPass = tex2D(_MainTex, i.basePos).a;
                half secondPass = tex2D(_MainTex, lerp(i.zoomPos, i.basePos, _Offset)).a;
                half4 output = half4(0,0,0,max(firstPass, secondPass));
                return output;
            }

        ENDCG
    }
}

构建工具不提供有关哪些脚本或行号出错的任何详细信息。有关Unity帖子效果的文档不包含任何有关细节的内容。 Google没有发现其他人遇到同样的问题。我发现的唯一的事情是有人建议SV_POSITION应该替换为POSITION,我已经尝试过各种各样的组合而没有成功。

我还在Unity中创建了一个新的图像效果着色器进行比较,代码看起来一样。

为后期效果创建合适的顶点着色器的正确形式是什么?或者甚至是我的代码的问题?

1 个答案:

答案 0 :(得分:3)

你关闭了! SV_POSITION语义用于传递给片段着色器的顶点位置。

SV_结构中的SV_POSITION中删除所有vertdata前缀。例如:

struct vertdata
{
    float2 uv : TEXCOORD0;
    float2 uvScaled : TEXCOORD1;
    float4 vertex : POSITION;    // <- Remove here
};

struct frag_v2f
{
    float4 vertex : SV_POSITION; // <- Keep here
    half2 uv : TEXCOORD0;
};