Cg Programming/Unity/Debugging of Shaders

From Wikibooks, open books for an open world
Jump to navigation Jump to search
A false-color satellite image.

This tutorial discusses vertex input parameters. It assumes that you are familiar with Section “Minimal Shader” and Section “RGB Cube”.

This tutorial also introduces the main technique to debug shaders in Unity: false-color images, i.e. a value is visualized by setting one of the components of the fragment color to it. Then the intensity of that color component in the resulting image allows you to make conclusions about the value in the shader. This might appear to be a very primitive debugging technique because it is a very primitive debugging technique. Unfortunately, there is no alternative in Unity.

Where Does the Vertex Data Come from?[edit | edit source]

In Section “RGB Cube” you have seen how the fragment shader gets its data from the vertex shader by means of an output structure of vertex output parameters. The question here is: where does the vertex shader get its data from? Within Unity, the answer is that the Mesh Renderer component of a game object sends all the data of the mesh of the game object to the GPU in each frame. (This is often called a “draw call”. Note that each draw call has some performance overhead; thus, it is much more efficient to send one large mesh with one draw call to the GPU than to send several smaller meshes with multiple draw calls.) This data usually consists of a long list of triangles, where each triangle is defined by three vertices and each vertex has certain attributes, including position. These attributes are made available in the vertex shader by means of vertex input parameters. The mapping of different attributes to different vertex input parameters is achieved in Cg by means of semantics, i.e. each vertex input parameter has to specify a certain semantic, e.g. POSITION, NORMAL, TEXCOORD0, TEXCOORD1, TANGENT, COLOR, etc. (In older versions of Unity, the built-in vertex input parameters also had to have specific names, namely the names that are used in this example.)

Built-in Vertex Input Parameters and how to Visualize Them[edit | edit source]

It is often convenient to included all input vertex parameters in a single structure, e.g.:

      struct vertexInput {
         float4 vertex : POSITION; // position (in object coordinates, 
            // i.e. local or model coordinates)
         float4 tangent : TANGENT;  
            // vector orthogonal to the surface normal
         float3 normal : NORMAL; // surface normal vector (in object
            // coordinates; usually normalized to unit length)
         float4 texcoord : TEXCOORD0;  // 0th set of texture 
            // coordinates (a.k.a. “UV”; between 0 and 1) 
         float4 texcoord1 : TEXCOORD1; // 1st set of tex. coors. 
         float4 texcoord2 : TEXCOORD2; // 2nd set of tex. coors. 
         float4 texcoord3 : TEXCOORD3; // 3rd set of tex. coors. 
         fixed4 color : COLOR; // color (usually constant)
      };

This structure could be used this way:

Shader "Cg shader with all built-in vertex input parameters" { 
   SubShader { 
      Pass { 
         CGPROGRAM 
 
         #pragma vertex vert  
         #pragma fragment frag 
 
         struct vertexInput {
            float4 vertex : POSITION;
            float4 tangent : TANGENT;  
            float3 normal : NORMAL;
            float4 texcoord : TEXCOORD0;  
            float4 texcoord1 : TEXCOORD1; 
            float4 texcoord2 : TEXCOORD2;  
            float4 texcoord3 : TEXCOORD3; 
            fixed4 color : COLOR; 
         };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 col : TEXCOORD0;
         };
 
         vertexOutput vert(vertexInput input) 
         {
            vertexOutput output;
 
            output.pos = UnityObjectToClipPos(input.vertex);
            output.col = input.texcoord; // set the output color

            // other possibilities to play with:

            // output.col = input.vertex;
            // output.col = input.tangent;
            // output.col = float4(input.normal, 1.0);
            // output.col = input.texcoord;
            // output.col = input.texcoord1;
            // output.col = input.texcoord2;
            // output.col = input.texcoord3;
            // output.col = input.color;

            return output;
         }
 
         float4 frag(vertexOutput input) : COLOR 
         {
            return input.col; 
         }
 
         ENDCG  
      }
   }
}

In Section “RGB Cube” we have already seen, how to visualize the vertex coordinates by setting the fragment color to those values. In this example, the fragment color is set to the texture coordinates such that we can see what kind of texture coordinates Unity provides.

Note that only the first three components of tangent represent the tangent direction. The scaling and the fourth component are set in a specific way, which is mainly useful for parallax mapping (see Section “Projection of Bumpy Surfaces”).

Pre-Defined Input Structures[edit | edit source]

Usually, you can achieve a higher performance by only specifying the vertex input parameters that you actually need, e.g. position, normal, and one set of texture coordinates; sometimes also the tangent vector. Unity provides the pre-defined input structures appdata_base, appdata_tan, appdata_full, and appdata_img for the most common cases. These are defined in the file UnityCG.cginc (in the directory Unity > Editor > Data > CGIncludes):

   struct appdata_base {
      float4 vertex : POSITION;
      float3 normal : NORMAL;
      float4 texcoord : TEXCOORD0;
   };
   struct appdata_tan {
      float4 vertex : POSITION;
      float4 tangent : TANGENT;
      float3 normal : NORMAL;
      float4 texcoord : TEXCOORD0;
   };
   struct appdata_full {
      float4 vertex : POSITION;
      float4 tangent : TANGENT;
      float3 normal : NORMAL;
      float4 texcoord : TEXCOORD0;
      float4 texcoord1 : TEXCOORD1;
      float4 texcoord2 : TEXCOORD2;
      float4 texcoord3 : TEXCOORD3;
      fixed4 color : COLOR;
      // and additional texture coordinates only on XBOX360
   };

   struct appdata_img {
      float4 vertex : POSITION;
      half2 texcoord : TEXCOORD0;
   };

The file UnityCG.cginc is included with the line #include "UnityCG.cginc". Thus, the shader above could be rewritten this way:

Shader "Cg shader with all built-in vertex input parameters" { 
   SubShader { 
      Pass { 
         CGPROGRAM 
 
         #pragma vertex vert  
         #pragma fragment frag 
         #include "UnityCG.cginc"
 
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 col : TEXCOORD0;
         };
 
         vertexOutput vert(appdata_full input) 
         {
            vertexOutput output;
 
            output.pos = UnityObjectToClipPos(input.vertex);
            output.col = input.texcoord;

            return output;
         }
 
         float4 frag(vertexOutput input) : COLOR 
         {
            return input.col; 
         }
 
         ENDCG  
      }
   }
}

How to Interpret False-Color Images[edit | edit source]

When trying to understand the information in a false-color image, it is important to focus on one color component only. For example, if the input vertex parameter texcoord with semantic TEXCOORD0 for a sphere is written to the fragment color then the red component of the fragment visualizes the x coordinate of texcoord, i.e. it doesn't matter whether the output color is maximum pure red or maximum yellow or maximum magenta, in all cases the red component is 1. On the other hand, it also doesn't matter for the red component whether the color is blue or green or cyan of any intensity because the red component is 0 in all cases. If you have never learned to focus solely on one color component, this is probably quite challenging; therefore, you might consider to look only at one color component at a time. For example by using this line to set the output parameter in the vertex shader:

            output.col = float4(input.texcoord.x, 0.0, 0.0, 1.0);

This sets the red component of the output parameter to the x component of texcoord but sets the green and blue components to 0 (and the alpha or opacity component to 1 but that doesn't matter in this shader).

If you focus on the red component or visualize only the red component you should see that it increases from 0 to 1 as you go around the sphere and after 360° drops to 0 again. It actually behaves similar to a longitude coordinate on the surface of a planet. (In terms of spherical coordinates, it corresponds to the azimuth.)

If the x component of texcoord corresponds to the longitude, one would expect that the y component would correspond to the latitude (or the inclination in spherical coordinates). However, note that texture coordinates are always between 0 and 1; therefore, the value is 0 at the bottom (south pole) and 1 at the top (north pole). You can visualize the y component as green on its own with:

            output.col = float4(0.0, input.texcoord.y, 0.0, 1.0);

Texture coordinates are particularly nice to visualize because they are between 0 and 1 just like color components are. Almost as nice are coordinates of normalized vectors (i.e., vectors of length 1; for example, the normal input parameter is usually normalized) because they are always between -1 and +1. To map this range to the range from 0 to 1, you add 1 to each component and divide all components by 2, e.g.:

            output.col = float4(
               (input.normal + float3(1.0, 1.0, 1.0)) / 2.0, 1.0);

Note that normal is a three-dimensional vector. Black corresponds then to the coordinate -1 and full intensity of one component to the coordinate +1.

If the value that you want to visualize is in another range than 0 to 1 or -1 to +1, you have to map it to the range from 0 to 1, which is the range of color components. If you don't know which values to expect, you just have to experiment. What helps here is that if you specify color components outside of the range 0 to 1, they are automatically clamped to this range. I.e., values less than 0 are set to 0 and values greater than 1 are set to 1. Thus, when the color component is 0 or 1 you know at least that the value is less or greater than what you assumed and then you can adapt the mapping iteratively until the color component is between 0 and 1.

Debugging Practice[edit | edit source]

In order to practice the debugging of shaders, this section includes some lines that produce black colors when the assignment to col in the vertex shader is replaced by each of them. Your task is to figure out for each line, why the result is black. To this end, you should try to visualize any value that you are not absolutely sure about and map the values less than 0 or greater than 1 to other ranges such that the values are visible and you have at least an idea in which range they are. Note that most of the functions and operators are documented in Section “Vector and Matrix Operations”.

            output.col = input.texcoord - float4(1.5, 2.3, 1.1, 0.0);
            output.col = input.texcoord.zzzz;
            output.col = input.texcoord / tan(0.0);

The following lines require some knowledge about the dot and cross product:

            output.col = dot(input.normal, input.tangent.xyz) *
               input.texcoord;
            output.col = dot(cross(input.normal, input.tangent.xyz),
               input.normal) * input.texcoord;
            output.col = float4(cross(input.normal, input.normal), 1.0);
            output.col = float4(cross(input.normal, 
               input.vertex.xyz), 1.0); 
               // only for a sphere!

Does the function radians() always return black? What's that good for?

            output.col = radians(input.texcoord);

Summary[edit | edit source]

Congratulations, you have reached the end of this tutorial! We have seen:

  • The list of built-in vertex input paramters in Unity.
  • How to visualize these parameters (or any other value) by setting components of the fragment output color.

Further reading[edit | edit source]

If you still want to know more

< Cg Programming/Unity

Unless stated otherwise, all example source code on this page is granted to the public domain.