Search Unity

How to print Shader's Var , please?

Discussion in 'Shaders' started by sfkdkjjj, Jul 6, 2009.

  1. sfkdkjjj

    sfkdkjjj

    Joined:
    Mar 31, 2009
    Posts:
    11
    thanks
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    What is Shader's Var?
     
  3. sfkdkjjj

    sfkdkjjj

    Joined:
    Mar 31, 2009
    Posts:
    11
    I want to print Shader variables,like to print javascript variables as.
    print(xxx);

    In order to see the value of a variable
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Like this?
    Code (csharp):
    1. print(material.GetFloat("foo"));
     
    BitGamey likes this.
  5. sfkdkjjj

    sfkdkjjj

    Joined:
    Mar 31, 2009
    Posts:
    11
    yes,like that
    Output a shader variable
     
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    So did I answer your question already?
     
  7. thedmd

    thedmd

    Guest

    I guess that sfkdkjjj want to print value of variable inside vertex/pixel shader.

    Usually in shaders You use colours or similar tricks to debbug your variables. It is not possible to see value directly.

    You can use Pix to see exact values. But it will not be what you expect. This is not even close to usual debbuging.
     
  8. sfkdkjjj

    sfkdkjjj

    Joined:
    Mar 31, 2009
    Posts:
    11
    Aras, thank for your answer,I am understand.
    And thank thedmd,as you puts it,your guess is very accurate.
    Thank you very mush
     
  9. danydavidpr

    danydavidpr

    Joined:
    Apr 2, 2015
    Posts:
    17
    hello how can i get the values in via print of a float4 variable within a shader script?
     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Short answer: You can't.

    Long answer: You'll have to debug shaders a different way.
    https://docs.unity3d.com/Manual/RenderDocIntegration.html
    https://docs.unity3d.com/Manual/SL-DebuggingD3D11ShadersWithVS.html

    If they're properties set directly on the material, you can get them from Material.GetVector or similar function like mentioned in the above thread. If they're values passed to the shader by internal Unity systems sometimes you can see them via the Frame Debugger. If they're calculated within the shader, you have to use one of the above tools. Most likely RenderDoc won't be enough.
     
    Last edited: Feb 1, 2017
    Sang_0 likes this.
  11. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328


    You can use following technique for pixel and compute shaders in DirectX 11 (link to source code in movie description).
    For vertex, hull and domain shaders, following technique works with DirectX 11.1+ GPU and Windows 8+.
     
  12. ghtx1138

    ghtx1138

    Joined:
    Dec 11, 2017
    Posts:
    114
    Hi @Przemyslaw_Zaworski thanks for your solution.

    Unfortunately I get the error:

    UnassignedReferenceException: The variable target of ShaderDebugging has not been assigned.
    You probably need to assign the target variable of the ShaderDebugging script in the inspector.

    I have a Quad in my scene with a ShaderDebugging material with a ShaderDebugging shader and a Mesh Renderer. And a GameObject with your script.

    Strangely it appears that render = target.GetComponent<Renderer>(); is not finding the Mesh Renderer.

    Would you have any ideas how I might fix this?

    Thanks
     
  13. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Strange, it should works.
    But you can add this small modification and then add script directly to quad (or any object with mesh filter and mesh renderer ):
    upload_2018-11-29_22-32-28.png
     
  14. ghtx1138

    ghtx1138

    Joined:
    Dec 11, 2017
    Posts:
    114
    Yep works perfectly. Thanks so much!

    Cheers
     
  15. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Unity vertex shader debugging (second technique, works directly without CS scripts), see line 67:

    Code (CSharp):
    1. Shader "Debugger"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Cull Off
    10.         Pass
    11.         {
    12.             CGPROGRAM
    13.             #pragma vertex VSMain
    14.             #pragma geometry GSMain
    15.             #pragma fragment PSMain
    16.             #pragma target 5.0
    17.  
    18.             sampler2D _MainTex;
    19.  
    20.             struct Data
    21.             {
    22.                 float4 vertex : SV_Position;
    23.                 float2 uv : TEXCOORD0;
    24.                 float number : VALUE;
    25.             };
    26.          
    27.             // "PrintValue" function by P.Malin
    28.             float PrintValue( float2 vCoords, float fValue, float fMaxDigits, float fDecimalPlaces )
    29.             {
    30.                 if ((vCoords.y < 0.0) || (vCoords.y >= 1.0)) return 0.0;
    31.                 bool bNeg = ( fValue < 0.0 );
    32.                 fValue = abs(fValue);
    33.                 float fBiggestIndex = max(floor(log2(abs(fValue)) / log2(10.0)), 0.0);
    34.                 float fDigitIndex = fMaxDigits - floor(vCoords.x);
    35.                 float fCharBin = 0.0;
    36.                 if(fDigitIndex > (-fDecimalPlaces - 1.01))
    37.                 {
    38.                     if(fDigitIndex > fBiggestIndex)
    39.                     {
    40.                         if((bNeg) && (fDigitIndex < (fBiggestIndex+1.5))) fCharBin = 1792.0;
    41.                     }
    42.                     else
    43.                     {
    44.                         if(fDigitIndex == -1.0)
    45.                         {
    46.                             if(fDecimalPlaces > 0.0) fCharBin = 2.0;
    47.                         }
    48.                         else
    49.                         {
    50.                             float fReducedRangeValue = fValue;
    51.                             if(fDigitIndex < 0.0) { fReducedRangeValue = frac( fValue ); fDigitIndex += 1.0; }
    52.                             float fDigitValue = (abs(fReducedRangeValue / (pow(10.0, fDigitIndex))));
    53.                             int x = int(floor(fDigitValue - 10.0 * floor(fDigitValue/10.0)));
    54.                             fCharBin = x==0?480599.0:x==1?139810.0:x==2?476951.0:x==3?476999.0:x==4?350020.0:x==5?464711.0:x==6?464727.0:x==7?476228.0:x==8?481111.0:x==9?481095.0:0.0;
    55.                         }
    56.                     }
    57.                 }
    58.                 float result = (fCharBin / pow(2.0, floor(frac(vCoords.x) * 4.0) + (floor(vCoords.y * 5.0) * 4.0)));
    59.                 return floor(result - 2.0 * floor(result/2.0));
    60.             }
    61.          
    62.             Data VSMain( float4 vertex:POSITION, float2 uv:TEXCOORD0 )
    63.             {
    64.                 Data VS;
    65.                 VS.uv = uv;
    66.                 VS.vertex = vertex;
    67.                 VS.number = 12.34;  //vertex shader variable value to print
    68.                 return VS;
    69.             }
    70.  
    71.             [maxvertexcount(9)]
    72.             void GSMain( triangle Data patch[3], inout TriangleStream<Data> stream, uint id:SV_PRIMITIVEID )
    73.             {
    74.                 Data GS;
    75.                 for (uint i = 0; i < 3; i++)
    76.                 {
    77.                     GS.vertex = UnityObjectToClipPos(patch[i].vertex);
    78.                     GS.uv = patch[i].uv;
    79.                     GS.number = patch[i].number;
    80.                     stream.Append(GS);
    81.                 }
    82.                 stream.RestartStrip();
    83.              
    84.                 if (id == 0)  // determine quad
    85.                 {
    86.                     for (uint i = 0; i < 6; i++)
    87.                     {
    88.                         float u = float(i) - 2.0 * floor(float(i)/2.0);
    89.                         float v = sign(fmod(126.0,fmod(float(i),6.0)+6.0));
    90.                         GS.uv = float2(u, 1.0 - v) + 10000.0;  // UV offset
    91.                         GS.vertex = float4(sign(u)+0.5, sign(v)+0.5, 0.3, 1.0);
    92.                         GS.number = patch[0].number;
    93.                         stream.Append(GS);
    94.                     }
    95.                 }
    96.                 stream.RestartStrip();
    97.             }
    98.  
    99.             float4 PSMain(Data PS) : SV_Target
    100.             {
    101.                 float value = PS.number;
    102.                 if (PS.uv.x > 9000.0)  // determine quad
    103.                 {
    104.                     float2 font = float2(24.0, 30.0);
    105.                     float2 position = float2(_ScreenParams.x - 250.0, 15.0);
    106.                     float3 base = PrintValue( (PS.vertex.xy - position) / font, value, 6.0, 2.0).xxx;
    107.                     return float4(base, 1.0);
    108.                 }
    109.                 else return tex2D(_MainTex, PS.uv);
    110.             }
    111.             ENDCG
    112.         }
    113.     }
    114. }
    upload_2019-11-10_20-14-9.png
     
  16. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    472
    Hello @
    Przemyslaw_Zaworski
    Please i am trying to use second technique but it doesn't work
    Does it work with Surface Shader ?
    thanks
     
  17. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Surface Shader doesn't support geometry shader stage (which I use to render a additional quad with visible number).
    So, with Surface Shader you should render digits somewhere directly on mesh (I choose top-right corner of plane mesh). Example for number from line 55:

    Code (CSharp):
    1. Shader "Debugger"
    2. {
    3.     Properties
    4.     {
    5.         [HideInInspector] _texcoord( "", 2D ) = "white" {}
    6.     }
    7.     Subshader
    8.     {
    9.         Tags { "RenderType" = "Opaque" }
    10.         CGPROGRAM
    11.         #pragma surface SurfaceShader Standard fullforwardshadows addshadow
    12.  
    13.         struct Input
    14.         {
    15.             float2 uv_texcoord;  //float2 uv_MainTex;
    16.         };
    17.  
    18.         // "PrintValue" function by P.Malin
    19.         float PrintValue( float2 vCoords, float fValue, float fMaxDigits, float fDecimalPlaces )
    20.         {
    21.             if ((vCoords.y < 0.0) || (vCoords.y >= 1.0)) return 0.0;
    22.             bool bNeg = ( fValue < 0.0 );
    23.             fValue = abs(fValue);
    24.             float fBiggestIndex = max(floor(log2(abs(fValue)) / log2(10.0)), 0.0);
    25.             float fDigitIndex = fMaxDigits - floor(vCoords.x);
    26.             float fCharBin = 0.0;
    27.             if(fDigitIndex > (-fDecimalPlaces - 1.01))
    28.             {
    29.                 if(fDigitIndex > fBiggestIndex)
    30.                 {
    31.                     if((bNeg) && (fDigitIndex < (fBiggestIndex+1.5))) fCharBin = 1792.0;
    32.                 }
    33.                 else
    34.                 {
    35.                     if(fDigitIndex == -1.0)
    36.                     {
    37.                         if(fDecimalPlaces > 0.0) fCharBin = 2.0;
    38.                     }
    39.                     else
    40.                     {
    41.                         float fReducedRangeValue = fValue;
    42.                         if(fDigitIndex < 0.0) { fReducedRangeValue = frac( fValue ); fDigitIndex += 1.0; }
    43.                         float fDigitValue = (abs(fReducedRangeValue / (pow(10.0, fDigitIndex))));
    44.                         int x = int(floor(fDigitValue - 10.0 * floor(fDigitValue/10.0)));
    45.                         fCharBin = x==0?480599.0:x==1?139810.0:x==2?476951.0:x==3?476999.0:x==4?350020.0:x==5?464711.0:x==6?464727.0:x==7?476228.0:x==8?481111.0:x==9?481095.0:0.0;
    46.                     }
    47.                 }
    48.             }
    49.             float result = (fCharBin / pow(2.0, floor(frac(vCoords.x) * 4.0) + (floor(vCoords.y * 5.0) * 4.0)));
    50.             return floor(result - 2.0 * floor(result/2.0));
    51.         }
    52.  
    53.         void SurfaceShader (Input IN, inout SurfaceOutputStandard o)
    54.         {
    55.             float2 uv = IN.uv_texcoord.xy;  //IN.uv_MainTex
    56.             float number = 123.0;
    57.             if (uv.x > 0.8 && uv.y > 0.8)
    58.             {
    59.                 float2 font = float2(36.0, 45.0);
    60.                 float2 position = float2(0.72,0.9);
    61.                 float3 base = PrintValue( (uv - position) * font, number, 6.0, 2.0).xxx;
    62.                 o.Albedo = float4(base, 1.0);
    63.             }
    64.             else
    65.                 o.Albedo = float4(uv, 0.0, 1.0);  //o.Albedo = tex2D(_MainTex,IN.uv_MainTex);
    66.             o.Normal = float3(0.0, 0.0, 1.0);
    67.             o.Metallic = 0.0;
    68.             o.Smoothness = 0.0;
    69.         }
    70.  
    71.         ENDCG
    72.     }
    73. }
    upload_2020-7-14_13-10-17.png