Search Unity

Grab Pass Causes Crash (5.3.2f1)

Discussion in 'Shaders' started by Ewanuk, Mar 1, 2016.

  1. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    In editor mode, when I make a change to the following shader, save, and return window focus to Unity, Unity crashes.

    If I comment out lines 29 - 33 (the grab pass), everything works fine. Any help? I read somewhere it could be related to something else in my scene using render textures. Is that a possible cause?

    Code (CSharp):
    1. Shader "WaterLAB/Balanced/Weir - Fluid Surface"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    6.         _NormalMap("Bump", 2D) = "bump" {}
    7.  
    8.         _MainTint("Diffuse Tint", Color) = (1,1,1,1)
    9.  
    10.         _SpecPower("Specular Power", Range(0.1, 500)) = 3
    11.  
    12.         //How much objects/light behind the water flow is "bent"
    13.         _RefractionAmount("Refraction Amount", Range(0, 100)) = 35
    14.  
    15.         //How much to multiply the diffuse color in greyscale value before assinging to the output alpha
    16.         _DiffToAlphaPower("Diffuse To Alpha Power (Lighting)", Range(0.5, 1.5)) = 1.5
    17.     }
    18.  
    19.     SubShader
    20.     {
    21.         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    22.         Cull Back
    23.         ZWrite Off
    24.         Blend SrcAlpha OneMinusSrcAlpha
    25.         LOD 200
    26.  
    27.         // This pass grabs the screen behind the object into a texture.
    28.         // We can access the result in the next pass as _GrabTexture
    29.         GrabPass
    30.         {
    31.             Name "BASE"
    32.             Tags{ "LightMode" = "Always" }
    33.         }
    34.  
    35.         // Refraction pass: Take the texture grabbed above and use the bumpmap to perturb it
    36.         //// on to the screen
    37.         //Pass
    38.         //{
    39.  
    40.         //    Name "BASE"
    41.         //    Tags{ "LightMode" = "Always" }
    42.  
    43.         //    CGPROGRAM
    44.         //    #pragma vertex vert
    45.         //    #pragma fragment frag
    46.         //    #pragma fragmentoption ARB_precision_hint_fastest
    47.         //    #include "UnityCG.cginc"
    48.  
    49.         //    struct appdata_t {
    50.         //        float4 vertex : POSITION;
    51.         //        float2 texcoord: TEXCOORD0;
    52.         //    };
    53.  
    54.         //    struct v2f {
    55.         //        float4 vertex : POSITION;
    56.         //        float4 uvgrab : TEXCOORD0;
    57.         //        float2 uvbump : TEXCOORD1;
    58.         //    };
    59.  
    60.  
    61.         //    sampler2D _NormalMap;
    62.         //    float4 _NormalMap_ST;
    63.         //    float _RefractionAmount;
    64.  
    65.         //    sampler2D _GrabTexture;
    66.         //    float4 _GrabTexture_TexelSize;
    67.  
    68.         //    v2f vert(appdata_t v)
    69.         //    {
    70.         //        v2f o;
    71.  
    72.         //        //Set vertex position from model space to screen space
    73.         //        o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    74.  
    75.         //        //Adjust for OpenGL/DirectX platforms
    76.         //        #if UNITY_UV_STARTS_AT_TOP
    77.         //                    float scale = -1.0;
    78.         //        #else
    79.         //                    float scale = 1.0;
    80.         //        #endif
    81.  
    82.         //        //Set UV Grab
    83.         //        o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    84.         //        o.uvgrab.zw = o.vertex.zw;
    85.  
    86.         //        //Set UV Bump, including the tiling/offset values given from the inspector
    87.         //        o.uvbump = TRANSFORM_TEX(v.texcoord, _NormalMap);
    88.  
    89.         //        return o;
    90.         //    }
    91.  
    92.         //    half4 frag(v2f i) : COLOR
    93.         //    {
    94.         //        // calculate perturbed coordinates
    95.  
    96.         //        // we could optimize this by just reading the x & y without reconstructing the Z in the UnpackNormal method (from the UnityCG.cginc)
    97.         //        half2 bump = UnpackNormal(tex2D(_NormalMap, i.uvbump)).rg;
    98.  
    99.         //        float2 offset = bump * _RefractionAmount * _GrabTexture_TexelSize.xy;
    100.         //        i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
    101.  
    102.  
    103.         //        half4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    104.  
    105.         //        return col;
    106.         //    }
    107.  
    108.         //    ENDCG
    109.         //}
    110.  
    111.         CGPROGRAM
    112.         #pragma surface surf CustomBlinnPhong alpha
    113.  
    114.         sampler2D _MainTex;
    115.         sampler2D _NormalMap;
    116.  
    117.         float4 _MainTint;
    118.  
    119.         float _SpecPower;
    120.         float _DiffToAlphaPower;
    121.  
    122.         struct Input
    123.         {
    124.             float2 uv_MainTex;
    125.         };
    126.  
    127.         void surf(Input IN, inout SurfaceOutput o)
    128.         {
    129.             half4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainTint;
    130.             o.Albedo = c.rgb;
    131.             o.Specular = _SpecPower;
    132.             o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
    133.         }
    134.  
    135.         inline fixed4 LightingCustomBlinnPhong(SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
    136.         {
    137.             float3 halfVector = normalize(lightDir + viewDir);
    138.  
    139.             float diff = max(0, dot(s.Normal, lightDir));
    140.  
    141.             float nh = max(0, dot(s.Normal, halfVector));
    142.             float spec = pow(nh, _SpecPower);
    143.  
    144.             fixed4 c;
    145.             c = fixed4(1, 0, 0, 1) * atten;
    146.             //c.rgb = (s.Albedo * _LightColor0.rgb * diff) + (_LightColor0.rgb * spec) * (atten * 2);
    147.             //c.a = pow((c.r + c.g + c.b) / 3, _DiffToAlphaPower);
    148.  
    149.             return c;
    150.         }
    151.  
    152.         ENDCG
    153.     }
    154.  
    155.     FallBack "Diffuse"
    156. }
     
  2. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    Update, it's now happening with other shaders, even worse, when I relaunch, my scene is reverted to the state it was when I launched unity last even if i saved it after the first launch. I've lost plenty of work because of this. Yet even worse, changes made to assets are also reverted to the state they were in at last launch. So now I may run into bugs as a result of changes to assets that were not preserved. This is a huge problem and is killing my productivity.

    Stack trace from the crash:

    Code (CSharp):
    1. 0x0000000140C2C971 (Unity) SelectionRenderQueue::Render
    2. 0x0000000140C2D08D (Unity) PickObjects
    3. 0x0000000140C2DF62 (Unity) PickClosestObject
    4. 0x0000000140C2E205 (Unity) PickClosestGO
    5. 0x000000014130DE77 (Unity) HandleUtility_CUSTOM_INTERNAL_CALL_Internal_PickClosestGO
    6. 0x0000000026BC6203 (Mono JIT Code) (wrapper managed-to-native) UnityEditor.HandleUtility:INTERNAL_CALL_Internal_PickClosestGO (UnityEngine.Camera,int,UnityEngine.Vector2&,UnityEngine.GameObject[],int&)
    7. 0x0000000026BC60EE (Mono JIT Code) [C:\buildslave\unity\build\artifacts\generated\common\editor\EditorHandlesUtilityBindings.gen.cs:536] UnityEditor.HandleUtility:Internal_PickClosestGO (UnityEngine.Camera,int,UnityEngine.Vector2,UnityEngine.GameObject[],int&)
    8. 0x0000000026BC5EB6 (Mono JIT Code) [C:\buildslave\unity\build\artifacts\generated\common\editor\EditorHandlesUtilityBindings.gen.cs:479] UnityEditor.HandleUtility:PickGameObject (UnityEngine.Vector2,UnityEngine.GameObject[],int&)
    9. 0x0000000026BC5960 (Mono JIT Code) [C:\buildslave\unity\build\artifacts\generated\common\editor\EditorHandlesUtilityBindings.gen.cs:493] UnityEditor.HandleUtility:PickGameObject (UnityEngine.Vector2,bool,UnityEngine.GameObject[])
    10. 0x0000000026BC5645 (Mono JIT Code) [C:\buildslave\unity\build\Editor\Mono\SceneView\SceneViewPicking.cs:84] UnityEditor.SceneViewPicking:GetAllOverlapping (UnityEngine.Vector2)
    11. 0x0000000026BC4F44 (Mono JIT Code) [C:\buildslave\unity\build\Editor\Mono\SceneView\SceneViewPicking.cs:27] UnityEditor.SceneViewPicking:PickGameObject (UnityEngine.Vector2)
    12. 0x0000000020074F1F (Mono JIT Code) [C:\buildslave\unity\build\Editor\Mono\SceneView\RectSelection.cs:143] UnityEditor.RectSelection:OnGUI ()
    13. 0x000000002007324A (Mono JIT Code) [C:\buildslave\unity\build\Editor\Mono\SceneView\SceneView.cs:1404] UnityEditor.SceneView:HandleSelectionAndOnSceneGUI ()
    14. 0x000000002004E546 (Mono JIT Code) [C:\buildslave\unity\build\Editor\Mono\SceneView\SceneView.cs:1242] UnityEditor.SceneView:OnGUI ()
    15. 0x000000001C38D422 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    16. 0x00007FFF7BAB41BF (mono) [c:\buildslave\mono-runtime-and-classlibs\build\mono\mini\mini.c:4937] mono_jit_runtime_invoke
    17. 0x00007FFF7BA08435 (mono) [c:\buildslave\mono-runtime-and-classlibs\build\mono\metadata\object.c:2623] mono_runtime_invoke
    18. 0x00007FFF7BA0E82F (mono) [c:\buildslave\mono-runtime-and-classlibs\build\mono\metadata\object.c:3827] mono_runtime_invoke_array
    19. 0x00007FFF7B9CCF2F (mono) [c:\buildslave\mono-runtime-and-classlibs\build\mono\metadata\icall.c:2857] ves_icall_InternalInvoke
    20. 0x000000001C370217 (Mono JIT Code) (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
    21. 0x000000001C36E372 (Mono JIT Code) [/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222] System.Reflection.MonoMethod:Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo)
    22. 0x000000001DA77B4F (Mono JIT Code) [/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115] System.Reflection.MethodBase:Invoke (object,object[])
    23. 0x0000000015B5F9C6 (Mono JIT Code) [C:\buildslave\unity\build\Editor\Mono\HostView.cs:187] UnityEditor.HostView:Invoke (string,object)
    24. 0x0000000015B5F8B0 (Mono JIT Code) [C:\buildslave\unity\build\Editor\Mono\HostView.cs:180] UnityEditor.HostView:Invoke (string)
    25. 0x000000001FFD9092 (Mono JIT Code) [C:\buildslave\unity\build\Editor\Mono\GUI\DockArea.cs:336] UnityEditor.DockArea:OnGUI ()
    26. 0x000000001C38D422 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    27. 0x00007FFF7BAB41BF (mono) [c:\buildslave\mono-runtime-and-classlibs\build\mono\mini\mini.c:4937] mono_jit_runtime_invoke
    28. 0x00007FFF7BA08435 (mono) [c:\buildslave\mono-runtime-and-classlibs\build\mono\metadata\object.c:2623] mono_runtime_invoke
    29. 0x000000014033E20F (Unity) scripting_method_invoke
    30. 0x00000001404A3EDC (Unity) ScriptingInvocationNoArgs::Invoke
    31. 0x00000001412058E6 (Unity) MonoBehaviourDoGUI
    32. 0x00000001411F8EC0 (Unity) IMGUIModule::MonoBehaviourDoGUI
    33. 0x000000014031F35A (Unity) MonoBehaviour::DoGUI
    34. 0x0000000140DAF8ED (Unity) GUIView::OnInputEvent
    35. 0x0000000140DB0A79 (Unity) GUIView::ProcessEventMessages
    36. 0x0000000140DB24F5 (Unity) GUIView::GUIViewWndProc
    37. 0x00007FFFAEAE250D (USER32) DispatchMessageW
    38. 0x00007FFFAEAE2367 (USER32) NotifyWinEvent
    39. 0x0000000140DCCDCB (Unity) RelaunchUnity
    40. 0x0000000140DCE45A (Unity) WinMain
    41. 0x000000014151FBB4 (Unity) read
    42. 0x00007FFFAE5A13D2 (KERNEL32) BaseThreadInitThunk
     
  3. jmargaris2

    jmargaris2

    Joined:
    Mar 20, 2015
    Posts:
    34
    I ran into a similar problem just now, figured out why my problem was so maybe this would be helpful for you.

    I have an effect that uses grabpass. The effect works by using Graphics.DrawMesh with the layer of the object it is attached to. What I found is that if the layer is "TransparentFX" it works, however if the layer is "Default" it instantly crashes.

    There are some other things going on in my game - there is a minimap camera and a camera that does a different grab pass / depth pass for some water drawing. Those cameras don't render TransparentFX layers.

    So it seems like the problem is that I have an effect that uses a grab pass, and that effect is ITSELF used by cameras that also use other grab passes. By making those effects not included in those other cameras by adjusting the layer the problem is fixed.

    Hope that helps.
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Could you submit a bug-report for this issue, please? Otherwise it's rather unlikely that Unity is going to fix this issue from my experience.