Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Shader Debugging Techniques

Discussion in 'Shaders' started by emmalloyd, Feb 12, 2014.

  1. emmalloyd

    emmalloyd

    Joined:
    Dec 5, 2013
    Posts:
    20
    I'm new to shaders so sorry if this is a noobish question, but I can't seem to find anything useful anywhere.

    Could one of you shader geniuses explain to me exactly how it is that you debug your shaders? I've seen comments where people say to output stuff as a color to a UV, but no one explains how to do it, or explain how to decipher the color information once it's done.

    What I would like to know is what techniques you use and exactly how do they work?
    Any help is greatly appreciated!

    Thank you for your time :)
     
    Buildabit likes this.
  2. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    I found that practicing a lot helps at the start. The more you understand what is going on within the shader the better you understand why something might be the wrong colour somewhere (black or white is usually the common colours things go I find). This is usually something with normals being inverted in places.

    Also, when people say output variables in the shader, this simply means return fixed4( colour you want here ). So if you have a complicated shader, go from top to bottom, outputting certain colours at specific intervals Output Diffuse->Diffuse+Spec->Diffuse+Refl and you can find what bits of code are causing issues with your shader.

    Obviously, when its an un-wanted bright pink, Unity will usually give you some error codes to work from which is a lot more helpful then when shaders compile and run but don't do exactly what you want.

    Also, for outputting UV/world positions/normals, the colours are usually the same throughout different objects (Normals/positons towards the camera are the same in different projects [If your code is correct obviously])
     
  3. emmalloyd

    emmalloyd

    Joined:
    Dec 5, 2013
    Posts:
    20
    Hi, thanks for your reply! I agree practicing does help a great deal. Could you give me an example of code using the technique? The thing I can't quite seem to get my head around is where/how to implement it properly within the shader.
    Thanks again! :)
     
    ricarious likes this.
  4. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Code (csharp):
    1.  
    2. Shader "Tutorial/Display Normals" {
    3. SubShader {
    4.     Pass {
    5.  
    6. CGPROGRAM
    7. #pragma vertex vert
    8. #pragma fragment frag
    9. #include "UnityCG.cginc"
    10.  
    11. struct v2f {
    12.     float4 pos : SV_POSITION;
    13.     float3 color : COLOR0;
    14. };
    15.  
    16. v2f vert (appdata_base v)
    17. {
    18.     v2f o;
    19.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    20.     o.color = v.normal * 0.5 + 0.5;
    21.     return o;
    22. }
    23.  
    24. half4 frag (v2f i) : COLOR
    25. {
    26.     return half4 (i.color, 1);
    27. }
    28. ENDCG
    29.  
    30.     }
    31. }
    32. Fallback "VertexLit"
    33. }
    This will output the normals of an object. It should make your object colour something like this. Normals of a surface that will be facing you are coloured more blue from that example and the colour changes as they face away from you.

    It's not the best method for debugging shaders though and can only tell you if imported models are wrong really, however, if you are calculating normals, you can output them through the fragment shader like that program and see if it shows you a similar colour representation.
     
  5. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    538
    You do it in the fragment shader, the output of which is the color that is written to the screen(before any post effects). There's an example in the docs that visualizes the normals of the model. Just keep in mind that the output of the fragment program is expected to be between 0-1(black to white), so it may be difficult to debug stuff not within this range.

    https://docs.unity3d.com/Documentation/Manual/ShaderTut2.html
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,356
    A more general, but nonetheless important debugging tip is to disable the Fallback command while writing a shader. This will prevent you from seeing the fallback shader when there are problems with the one you're writing.
     
    iSinner likes this.
  7. emmalloyd

    emmalloyd

    Joined:
    Dec 5, 2013
    Posts:
    20
    Thank you for all of your responses! One more quick question about the color debugging technique: Is it just used for visualising calculations or for seeing when things aren't working properly? Just wondering about some of the uses of it? :)
     
  8. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Shaders can be notoriously difficult to debug, especially when you are not familiar with them and programs like PIX that do debug shaders may seem to give you data you won't understand.

    That's why people opt to output given colours at certain points through the shader. For example, your specular lighting calculations doesn't seem to be correct - You can output the normal that you are using to find specular power at a given pixel and if that is coming out wrong, then you will need to change the way that normal is calculated and fix it.

    I also find that multiplying by a given variable in the inspector when the game is running can be helpful, as you can manipulate values to higher/lower intensities and watch the results change. So if you are trying to do normal mapping and it doesn't seem right, try multiplying your normal depth value by a given amount when running the code and can see if the illusion of depth is actually changing or whether there is something wrong in the calculations.
     
  9. emmalloyd

    emmalloyd

    Joined:
    Dec 5, 2013
    Posts:
    20
    I didn't realise there were debugging programs for shaders, I suppose there aren't any that you suggest then? lol. Thanks again for all your help!
     
  10. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    AFAIK there aren't any programs to debug ShaderLab code at the moment, but I may be wrong. PIX is just a tool for DirectX stuff really.
     
  11. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,342
  12. ArminRigo

    ArminRigo

    Joined:
    May 19, 2017
    Posts:
    8
  13. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    195
  14. FlorianBernard

    FlorianBernard

    Joined:
    Jun 9, 2015
    Posts:
    107
    Thanks for shareing. I'm trying to use your shader debugger but I get this:
    Code (CSharp):
    1. Shader error in 'Plant': ps_4_0 does not support interlocked operations at Assets/ShaderDebugger/debugger.cginc(47) (on d3d11)
    Any idea?
     
  15. jmo7

    jmo7

    Joined:
    Sep 21, 2018
    Posts:
    1
    I also got the error in '<FILENAME>': ps_4_0 does not support interlocked operations at Assets/ShaderDebugger/debugger.cginc(47) (on d3d11)
     
unityunity