• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by voldemarz · Nov 13, 2011 at 01:18 PM · camerashadersshaderlabcgdepth buffer

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: alt text

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;
 }


Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
NPS
damiant
LyleMakesGames

People who like this

3 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges