Search Unity

Unity Freezes after Mistakes in Shaders

Discussion in 'Shaders' started by the_lemur, Oct 20, 2016.

  1. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    I'm new to shaders and so I make a lot of mistakes -- which aren't even very obvious to me.. and when I switch from Visual Studio to Unity, Unity freezes up and won't let me do anything. No error messages are generated.

    There is an _Extremely_ wide range of things I can do that will make this happen. Just recently, it was because I tried to give a variable within CGPROGRAM, but outside of any vertex shader, an intial assignment.

    Apparently this isn't allowed, but there is no clear reason why, or any documentation on it. I've pretty much froze unity about 30 times since I started trying shaders.
     
  2. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    Just to make it clear, I'm not saying I'm not making errors. I'm making them all over the place. The problem is that a huge variety of Shader coding errors cause Unity to freeze up, and I have to restart.
     
  3. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    Also when I re-open the scene, my custom shaders (even the working ones I hadn't touched) are no longer assigned to the correct materials, and the materials aren't assigned to the objects. And yes I periodically Ctrl-S save scene so it's not that.

    Sometimes the correct scene isn't even loaded, Camera view settings are back to old values, changes to project settings ( even very old changes ) are undone.
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Don't treat shaders as if they are C#/Javascript code, shader code is very different and much more limited in what syntax sugar it has, such as initial assignment. Watch some tutorials on HLSL programming to understand what you can do. Unity isn't going to provide extensive documentation on shader programming, it already exists others places since it applies to all game engines. They merely provide documentation on the ShaderLab container they use and various Unity specific shaders extensions and such.
     
  5. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    You're missing the point which is that Unity is freezing over and over again. A mistake in a shader shouldn't hang unity.

    However it seems it might have been a VisualStudio plugin that imported for some reason. It hasn't happened since I deleted that, but it's too soon to say.
     
  6. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
    I make mistakes in shaders all the time and have never experienced this - generally any material using that shader just turns pink and you get an error logged in the inspector.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    I use Sublime Text instead of Visual Studio for writing shaders and have never had the Unity Editor hang on me from shader changes. My best guess was VS was attached to the editor and was catching the shader error causing the editor to pause. Usually it's pretty clear that's what's happening in VS though. Not sure what plugin was causing your problem.
     
  8. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    I would consider this a bug in Unity. Having an invalid .shader file should not freeze the editor, but output an error to the Console instead. If you still have these .shader files around, I recommend to submit a bug-report for that.

    I submitted a bug-report regarding an editor freeze caused by an invalid shader just yesterday.
    Having a closing curly brace too much in a surface shader, causes the editor to freeze, while the shader gets (re)imported.

    Reproduce:
    * Open provided project
    * Open Assets/NewSurfaceShader.shader in text editor
    * Change "#define BUG 0" to "#define BUG 1"
    * Save the .shader file
    * Switch back to Unity to reimport the shader
    => Observe that the editor freezes

    I was able to reproduce the issue with Unity 5.5.0b8 and 5.4.1.

    Expected:
    The editor should not freeze, but display a shader compile error.

    Code (CSharp):
    1. Shader "Custom/NewSurfaceShader" {
    2.    Properties {
    3.      _Color ("Color", Color) = (1,1,1,1)
    4.      _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.      _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.      _Metallic ("Metallic", Range(0,1)) = 0.0
    7.    }
    8.    SubShader {
    9.      Tags { "RenderType"="Opaque" }
    10.      LOD 200
    11.  
    12.      CGPROGRAM
    13. #define BUG 1
    14.  
    15.      // Physically based Standard lighting model, and enable shadows on all light types
    16.      #pragma surface surf Standard fullforwardshadows
    17.  
    18.      // Use shader model 3.0 target, to get nicer looking lighting
    19.      #pragma target 3.0
    20.  
    21.      sampler2D _MainTex;
    22.  
    23.      struct Input {
    24.        float2 uv_MainTex;
    25.      };
    26.  
    27.      half _Glossiness;
    28.      half _Metallic;
    29.      fixed4 _Color;
    30.  
    31.      void surf(Input IN, inout SurfaceOutputStandard o) {
    32.        // Albedo comes from a texture tinted by color
    33.        fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    34.        o.Albedo = c.rgb;
    35.  
    36. #if BUG
    37.      }
    38. #endif
    39.  
    40.  
    41.        // Metallic and smoothness come from slider variables
    42.        o.Metallic = _Metallic;
    43.        o.Smoothness = _Glossiness;
    44.        o.Alpha = c.a;
    45.      }
    46.      ENDCG
    47.    }
    48.    FallBack "Diffuse"
    49. }
    50.  
     
    doomtoo likes this.
  9. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    Hey Peter77. I should mention that there was a VisualStudio integration plugin in my Standard Assets Folder. I do not know how it got there, and if it was a Unity Standard Asset, or one I downloaded separately years ago.

    Either way, after I deleted that plugin, I have not seen any more errors as of yet. Do you have this VS plugin?

     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    @Peter77 - That shader also hangs Unity for me (5.4.1), and it's definitely not related to a Visual Studio thing. Quite curious as I'm sure I've made that error in the past and not had it hang so it might be a relatively new issue.
     
    Harinezumi likes this.
  11. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    Anybody still experiencing this in Unity 2018? Some errors are freezing up the editor on my machine and it's pretty difficult to debug them since I'm not seeing the line numbers or anything. Happens with and without Visual Studio being open.
     
    RockSPb likes this.
  12. RockSPb

    RockSPb

    Joined:
    Feb 6, 2015
    Posts:
    112
    The same problem here(Looks like unity doesn't attach an "error" (pink) shader instead of a shader that failed to compile. Put copy right in to the project, but it help only one time... Now I write shaders careful))
     
  13. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    Currently my (really frustrating) workaround is to restart Unity every time I have an error. Most of the time, the editor will start up frozen but at least listing the errors it's running into. Though there are certain situations when the editor just doesn't render at all...
     
  14. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Unity Technologies fixed the problem that was originally reported in this thread:
    https://issuetracker.unity3d.com/is...ng-curly-bracket-in-the-shader-freezes-editor

    If you're (still) able to reproduce the issue, it would help if could you submit a bug-report as described in this document:
    https://unity3d.com/unity/qa/bug-reporting

    It's important that you report these issues together with a reproduction project if you want them to get fixed, like I did earlier as mentioned here. If you don't do it, it might be a long time until someone else reports them or until Unity Technologies find them.

    After you submitted the bug-report, you receive a confirmation email with a bug-report Case number. Please post the Case number (number only, not the link) in this forum thread for Unity staff to pick up.
     
  15. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    Is there a problem with this pattern? It seems logically like it should work but I'm really struggling not to get Unity to lock up when doing it. Don't believe there are other code errors but the locking up makes that theory difficult to test.

    Code (csharp):
    1. #pragma shader_feature MY_TESSELLATION
    2. #pragma vertex vert
    3. #pragma fragment frag
    4.  
    5. #if (MY_TESSELLATION)
    6.     #pragma hull my_hull
    7.     #pragma domain my_domain
    8. #endif
    9.  
    10.  
    11. #if (MY_TESSELLATION)
    12.     hullInput vert() ...
    13. #else
    14.     fragInput vert() ...
    15. #endif
    16.  
    17.  
     
    Last edited: Dec 6, 2019
  16. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    I always find it difficult to create minimal repro cases for these things as the shader I'm working with is pretty complex. But I will try to see if I can get one going.