- Home /
 
Weird shader error X5204 - read of uninitialized components
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. (sorry for cross-posting here and in the forum) 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
 using UnityEngine;
 
 [RequireComponent(typeof(Camera))]
 public class Test : ImageEffectBase
 {
 
     public RenderTexture texDepth;
 
     void Awake()
     {
         Camera.main.depthTextureMode = DepthTextureMode.Depth;
         texDepth = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.Depth);
     }
 
     public void OnRenderImage(RenderTexture source, RenderTexture target)
     {
         // source ignored, material's shader outputs depth to target (screen)
         Graphics.Blit(source, target, material);
 
         // texDepth is completely black in Inspector
         //Graphics.Blit(source, texDepth, material);        
         //Graphics.Blit(texDepth, target);                
     }
 }
 
               Here's image effect shader which is outputing just depth:
 Shader "Depth test" {
     SubShader { 
         Pass {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag    
             #include "UnityCG.cginc"
 
             struct appdata_t {
                 float4 vertex : POSITION;
                 float2 texcoord : TEXCOORD0;
             };
     
             struct v2f {
                 float4 pos : POSITION;
                 float2 texcoord : TEXCOORD0;
             };
             
             
             v2f vert (appdata_t v)
             {
                 v2f o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.texcoord = v.texcoord;
                                 
                 return o;
             }
     
             sampler2D _MainTex;
             uniform sampler2D _CameraDepthTexture;
             
             half4 frag (v2f i) : COLOR
             {
                 float sceneZ = tex2D(_CameraDepthTexture, i.texcoord).x;
             
                 // if uncommented getting error
                 // "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"
                 //if (sceneZ < 1f)
                 //    return half4(0,0.5,0.5,1);
 
                 return tex2D(_CameraDepthTexture, i.texcoord);
 
             }
 
             ENDCG 
         } 
     }
 
     Fallback off
 }
 
               Here is how the scene looks when value from depth buffer is sent directly to the screen: 
Another thing puzzling me is that very similar fragment shader to the one above actually works fine in one different case:
 half4 frag (v2f i) : COLOR
 {
     half4 col = tex2D(_MainTex, i.texcoord);            
     float compositZ = tex2D(_CompositTex, i.texcoord).r;                
     float sceneZ = tex2D(_CameraDepthTexture, i.texcoord).r;
 
     if (sceneZ < compositZ && col.a > 0f)
         return half4(col.rgb, 0);
     else
         return col;
 }
 
              Answer by Joshua 3 · Jan 16, 2012 at 11:02 PM
I had the same error message and if it is the same problem to fix it you have to change
 if (sceneZ < 1f)
   return half4(0,0.5,0.5,1);
 
 return tex2D(_CameraDepthTexture, i.texcoord);
 
               to
 if (sceneZ < 1f)
   return half4(0,0.5,0.5,1);
 else
   return tex2D(_CameraDepthTexture, i.texcoord);
 
               I don't know why this is. Maybe some strange compiler optimization bug.
Your answer
 
             Follow this Question
Related Questions
Obtaining relative speed of vertex in a shader 1 Answer
Can this shader run in unity? 1 Answer
Separate texture coordinates in shaders 1 Answer
Uniform variable operations not for every vertex and fragment. 0 Answers
Porting from ShaderToy(GLSL) to shaderlab(HLSL/CG) unity not giving me the desired result. 2 Answers