CopyStd.shader 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Shader "Hidden/PostProcessing/CopyStd"
  2. {
  3. //
  4. // We need this shader for the very first RT blit using the internal CommandBuffer.Blit() method
  5. // so it can handle AAResolve properly. We also need it to be separate because of VR and the
  6. // need for a Properties block. If we were to add this block to the other Copy shader it would
  7. // not allow us to manually bind _MainTex, thus breaking a few other things in the process...
  8. //
  9. Properties
  10. {
  11. _MainTex ("", 2D) = "white" {}
  12. }
  13. CGINCLUDE
  14. struct Attributes
  15. {
  16. float4 vertex : POSITION;
  17. float2 texcoord : TEXCOORD0;
  18. };
  19. struct Varyings
  20. {
  21. float4 vertex : SV_POSITION;
  22. float2 texcoord : TEXCOORD0;
  23. };
  24. sampler2D _MainTex;
  25. float4 _MainTex_ST;
  26. Varyings Vert(Attributes v)
  27. {
  28. Varyings o;
  29. o.vertex = float4(v.vertex.xy * 2.0 - 1.0, 0.0, 1.0);
  30. o.texcoord = v.texcoord;
  31. #if UNITY_UV_STARTS_AT_TOP
  32. o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
  33. #endif
  34. o.texcoord = o.texcoord * _MainTex_ST.xy + _MainTex_ST.zw; // We need this for VR
  35. return o;
  36. }
  37. float4 Frag(Varyings i) : SV_Target
  38. {
  39. float4 color = tex2D(_MainTex, i.texcoord);
  40. return color;
  41. }
  42. //>>> We don't want to include StdLib.hlsl in this file so let's copy/paste what we need
  43. bool IsNan(float x)
  44. {
  45. return (x < 0.0 || x > 0.0 || x == 0.0) ? false : true;
  46. }
  47. bool AnyIsNan(float4 x)
  48. {
  49. return IsNan(x.x) || IsNan(x.y) || IsNan(x.z) || IsNan(x.w);
  50. }
  51. //<<<
  52. float4 FragKillNaN(Varyings i) : SV_Target
  53. {
  54. float4 color = tex2D(_MainTex, i.texcoord);
  55. if (AnyIsNan(color))
  56. {
  57. color = (0.0).xxxx;
  58. }
  59. return color;
  60. }
  61. ENDCG
  62. SubShader
  63. {
  64. Cull Off ZWrite Off ZTest Always
  65. // 0 - Copy
  66. Pass
  67. {
  68. CGPROGRAM
  69. #pragma vertex Vert
  70. #pragma fragment Frag
  71. ENDCG
  72. }
  73. // 1 - Copy + NaN killer
  74. Pass
  75. {
  76. CGPROGRAM
  77. #pragma vertex Vert
  78. #pragma fragment FragKillNaN
  79. ENDCG
  80. }
  81. }
  82. }