Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Weird shader error X5204 - read of uninitialized components

Discussion in 'Scripting' started by voldemarz, Nov 11, 2011.

  1. voldemarz

    voldemarz

    Joined:
    Sep 19, 2011
    Posts:
    25
    EDIT: oops, the thread probably should have been it ShaderLab section.

    1) In Camera.OnRenderImage(..) I'm using camera's depth buffer to determine if certain areas of the image should be drawn. I ran into weird problem where I get shader error when IF statement is used to check the depth value. Can anyone explain what am I doing wrong?

    I attached a unity package with scene, script and shader so that anybody can try to run it. When the IF statement in the fragment shader below is uncommented I get following error in the console:

    Shader error in 'Depth test': D3D shader assembly failed with: (11): error X5204: Read of uninitialized components(*) in r1: *r/x/0 *g/y/1 *b/z/2 *a/w/3

    Shader Assembly: ps_2_0
    ; 8 ALU, 1 TEX
    dcl_2d s0
    def c0, -1.00000000, 1.00000000, 0.00000000, 0.50000000
    dcl t0.xy
    texld r3, t0, s0
    add r0.x, r3, c0
    mov r2.yz, c0.w
    mov r2.x, c0.z
    mov r2.w, c0.y
    cmp_pp r1, r0.x, r1, r2
    cmp_pp r0.x, r0, c0.y, c0.z
    cmp_pp r0, -r0.x, r1, r3
    mov_pp oC0, r0


    2) Another mystery for me is why I don't get depth rendered on the screen (get black screen) when I first output depth to texDepth RenderTexture and only then to the screen (target). I'd be grateful if anybody could explain what is wrong in these scripts. I


    Here's C# script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(Camera))]
    6. public class Test : ImageEffectBase {
    7.    
    8.     public RenderTexture texDepth;
    9.        
    10.     void Awake () {
    11.         Camera.main.depthTextureMode = DepthTextureMode.Depth;
    12.         texDepth = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.Depth);
    13.     }
    14.    
    15.     public void OnRenderImage(RenderTexture source, RenderTexture target)
    16.     {  
    17.         // source ignored, material's shader outputs depth to target (screen)
    18.         Graphics.Blit(source, target, material);       
    19.        
    20.         // texDepth is completely black in Inspector
    21.         //Graphics.Blit(source, texDepth, material);       
    22.         //Graphics.Blit(texDepth, target);             
    23.     }
    24. }
    Here's image effect shader which is outputing just depth:
    Code (csharp):
    1.  
    2. Shader "Depth test" {
    3.     SubShader {
    4.         Pass {
    5.             CGPROGRAM
    6.             #pragma vertex vert
    7.             #pragma fragment frag  
    8.             #include "UnityCG.cginc"
    9.  
    10.             struct appdata_t {
    11.                 float4 vertex : POSITION;
    12.                 float2 texcoord : TEXCOORD0;
    13.             };
    14.    
    15.             struct v2f {
    16.                 float4 pos : POSITION;
    17.                 float2 texcoord : TEXCOORD0;
    18.             };
    19.            
    20.            
    21.             v2f vert (appdata_t v)
    22.             {
    23.                 v2f o;
    24.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    25.                 o.texcoord = v.texcoord;
    26.                                
    27.                 return o;
    28.             }
    29.    
    30.             sampler2D _MainTex;
    31.             uniform sampler2D _CameraDepthTexture;
    32.            
    33.             half4 frag (v2f i) : COLOR
    34.             {
    35.                 float sceneZ = tex2D(_CameraDepthTexture, i.texcoord).x;
    36.            
    37.                 // if uncommented getting error
    38.                 // "D3D shader assembly failed with: (11): error X5204: Read of uninitialized components(*) in r1: *r/x/0 *g/y/1 *b/z/2 *a/w/3"
    39.                 //if (sceneZ < 1f)
    40.                 //  return half4(0,0.5,0.5,1);
    41.  
    42.                 return tex2D(_CameraDepthTexture, i.texcoord);
    43.  
    44.             }
    45.  
    46.             ENDCG
    47.         }
    48.     }
    49.  
    50. Fallback off
    51. }
    52.  

    Camera's depth buffer sent directly to the screen:
    $sceneDepth.JPG


    Another thing puzzling me is that very similar fragment shader to the one above actually works fine in one different case:
    Code (csharp):
    1. half4 frag (v2f i) : COLOR
    2. {
    3.     half4 col = tex2D(_MainTex, i.texcoord);           
    4.     float compositZ = tex2D(_CompositTex, i.texcoord).r;               
    5.     float sceneZ = tex2D(_CameraDepthTexture, i.texcoord).r;
    6.  
    7.     if (sceneZ < compositZ  col.a > 0f)
    8.         return half4(col.rgb, 0);
    9.     else
    10.         return col;
    11. }
     

    Attached Files:

    Last edited: Nov 13, 2011
  2. voldemarz

    voldemarz

    Joined:
    Sep 19, 2011
    Posts:
    25
    Come on.. isn't there anybody who could help? I even provided test scripts so that you could see the error yourselves...
     
  3. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    I'm getting the same problem. This seriously hasnt been answered?
     
  4. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    519
    It's quite strange. I've just got the same problem when i tried to run my app on Win. on MacOS it works well.


    upd1:

    I changed my fragment shader from this
    Code (csharp):
    1.  
    2.             half4 frag (v2f IN) : COLOR
    3.             {
    4.                 half4 col     = tex2D(_MainTex, IN.texcoord);                  
    5.                 half4 maskCol = tex2D(_MainTex, IN.texcoord1);                 
    6.                 if (maskCol.a != 0)                
    7.                     return half4( lerp(col.rgb, col.rgb * _Color.rgb, col.a), col.a );
    8.                 return col;
    9.             }
    10.  
    to this

    Code (csharp):
    1.  
    2.             half4 frag (v2f IN) : COLOR
    3.             {
    4.                 half4 col     = tex2D(_MainTex, IN.texcoord);                  
    5.                 half4 maskCol = tex2D(_MainTex, IN.texcoord1);                 
    6.                 if (maskCol.a != 0)                
    7.                     col = half4( lerp(col.rgb, col.rgb * _Color.rgb, col.a), col.a );
    8.                 return col;
    9.             }
    10.  
    And now it works well. I don't know why is not possible to use return half4(...) on windows in fragment shader.
     
    Last edited: Jun 6, 2013
unityunity