FXAA2.shader 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Hidden/FXAA II" {
  3. Properties {
  4. _MainTex ("Base (RGB)", 2D) = "white" {}
  5. }
  6. SubShader {
  7. Pass {
  8. ZTest Always Cull Off ZWrite Off
  9. CGPROGRAM
  10. #pragma vertex vert
  11. #pragma fragment frag
  12. #include "UnityCG.cginc"
  13. #pragma target 3.0
  14. #define FXAA_HLSL_3 1
  15. /*============================================================================
  16. FXAA v2 CONSOLE by TIMOTHY LOTTES @ NVIDIA
  17. ============================================================================*/
  18. /*============================================================================
  19. API PORTING
  20. ============================================================================*/
  21. #ifndef FXAA_GLSL_120
  22. #define FXAA_GLSL_120 0
  23. #endif
  24. #ifndef FXAA_GLSL_130
  25. #define FXAA_GLSL_130 0
  26. #endif
  27. #ifndef FXAA_HLSL_3
  28. #define FXAA_HLSL_3 0
  29. #endif
  30. #ifndef FXAA_HLSL_4
  31. #define FXAA_HLSL_4 0
  32. #endif
  33. /*--------------------------------------------------------------------------*/
  34. #if FXAA_GLSL_120
  35. // Requires,
  36. // #version 120
  37. // #extension GL_EXT_gpu_shader4 : enable
  38. #define int2 ivec2
  39. #define float2 vec2
  40. #define float3 vec3
  41. #define float4 vec4
  42. #define FxaaInt2 ivec2
  43. #define FxaaFloat2 vec2
  44. #define FxaaSat(a) clamp((a), 0.0, 1.0)
  45. #define FxaaTex sampler2D
  46. #define FxaaTexLod0(t, p) texture2DLod(t, p, 0.0)
  47. #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
  48. #endif
  49. /*--------------------------------------------------------------------------*/
  50. #if FXAA_GLSL_130
  51. // Requires "#version 130" or better
  52. #define int2 ivec2
  53. #define float2 vec2
  54. #define float3 vec3
  55. #define float4 vec4
  56. #define FxaaInt2 ivec2
  57. #define FxaaFloat2 vec2
  58. #define FxaaSat(a) clamp((a), 0.0, 1.0)
  59. #define FxaaTex sampler2D
  60. #define FxaaTexLod0(t, p) textureLod(t, p, 0.0)
  61. #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o)
  62. #endif
  63. /*--------------------------------------------------------------------------*/
  64. #if FXAA_HLSL_3
  65. #define int2 float2
  66. #define FxaaInt2 float2
  67. #define FxaaFloat2 float2
  68. #define FxaaSat(a) saturate((a))
  69. #define FxaaTex sampler2D
  70. #define FxaaTexLod0(t, p) tex2Dlod(t, float4(p, 0.0, 0.0))
  71. #define FxaaTexOff(t, p, o, r) tex2Dlod(t, float4(p + (o * r), 0, 0))
  72. #endif
  73. /*--------------------------------------------------------------------------*/
  74. #if FXAA_HLSL_4
  75. #define FxaaInt2 int2
  76. #define FxaaFloat2 float2
  77. #define FxaaSat(a) saturate((a))
  78. struct FxaaTex { SamplerState smpl; Texture2D tex; };
  79. #define FxaaTexLod0(t, p) t.tex.SampleLevel(t.smpl, p, 0.0)
  80. #define FxaaTexOff(t, p, o, r) t.tex.SampleLevel(t.smpl, p, 0.0, o)
  81. #endif
  82. /*============================================================================
  83. VERTEX SHADER
  84. ============================================================================*/
  85. float4 FxaaVertexShader(
  86. float2 pos, // Both x and y range {-1.0 to 1.0 across screen}.
  87. float2 rcpFrame) { // {1.0/frameWidth, 1.0/frameHeight}
  88. /*--------------------------------------------------------------------------*/
  89. #define FXAA_SUBPIX_SHIFT (1.0/4.0)
  90. /*--------------------------------------------------------------------------*/
  91. float4 posPos;
  92. posPos.xy = (pos.xy * 0.5) + 0.5;
  93. posPos.zw = posPos.xy - (rcpFrame * (0.5 + FXAA_SUBPIX_SHIFT));
  94. return posPos; }
  95. /*============================================================================
  96. PIXEL SHADER
  97. ============================================================================*/
  98. float3 FxaaPixelShader(
  99. float4 posPos, // Output of FxaaVertexShader interpolated across screen.
  100. FxaaTex tex, // Input texture.
  101. float2 rcpFrame) { // Constant {1.0/frameWidth, 1.0/frameHeight}.
  102. /*--------------------------------------------------------------------------*/
  103. #define FXAA_REDUCE_MIN (1.0/128.0)
  104. #define FXAA_REDUCE_MUL (1.0/8.0)
  105. #define FXAA_SPAN_MAX 8.0
  106. /*--------------------------------------------------------------------------*/
  107. float3 rgbNW = FxaaTexLod0(tex, posPos.zw).xyz;
  108. float3 rgbNE = FxaaTexOff(tex, posPos.zw, FxaaInt2(1,0), rcpFrame.xy).xyz;
  109. float3 rgbSW = FxaaTexOff(tex, posPos.zw, FxaaInt2(0,1), rcpFrame.xy).xyz;
  110. float3 rgbSE = FxaaTexOff(tex, posPos.zw, FxaaInt2(1,1), rcpFrame.xy).xyz;
  111. float3 rgbM = FxaaTexLod0(tex, posPos.xy).xyz;
  112. /*--------------------------------------------------------------------------*/
  113. float3 luma = float3(0.299, 0.587, 0.114);
  114. float lumaNW = dot(rgbNW, luma);
  115. float lumaNE = dot(rgbNE, luma);
  116. float lumaSW = dot(rgbSW, luma);
  117. float lumaSE = dot(rgbSE, luma);
  118. float lumaM = dot(rgbM, luma);
  119. /*--------------------------------------------------------------------------*/
  120. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  121. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  122. /*--------------------------------------------------------------------------*/
  123. float2 dir;
  124. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  125. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  126. /*--------------------------------------------------------------------------*/
  127. float dirReduce = max(
  128. (lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL),
  129. FXAA_REDUCE_MIN);
  130. float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce);
  131. dir = min(FxaaFloat2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),
  132. max(FxaaFloat2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
  133. dir * rcpDirMin)) * rcpFrame.xy;
  134. /*--------------------------------------------------------------------------*/
  135. float3 rgbA = (1.0/2.0) * (
  136. FxaaTexLod0(tex, posPos.xy + dir * (1.0/3.0 - 0.5)).xyz +
  137. FxaaTexLod0(tex, posPos.xy + dir * (2.0/3.0 - 0.5)).xyz);
  138. float3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
  139. FxaaTexLod0(tex, posPos.xy + dir * (0.0/3.0 - 0.5)).xyz +
  140. FxaaTexLod0(tex, posPos.xy + dir * (3.0/3.0 - 0.5)).xyz);
  141. float lumaB = dot(rgbB, luma);
  142. if((lumaB < lumaMin) || (lumaB > lumaMax)) return rgbA;
  143. return rgbB; }
  144. struct v2f {
  145. float4 pos : SV_POSITION;
  146. float4 uv : TEXCOORD0;
  147. };
  148. float4 _MainTex_TexelSize;
  149. v2f vert (appdata_img v)
  150. {
  151. v2f o;
  152. o.pos = UnityObjectToClipPos (v.vertex);
  153. o.uv = FxaaVertexShader (v.texcoord.xy*2-1, _MainTex_TexelSize.xy);
  154. return o;
  155. }
  156. sampler2D _MainTex;
  157. float4 frag (v2f i) : SV_Target
  158. {
  159. return float4(FxaaPixelShader(i.uv, _MainTex, _MainTex_TexelSize.xy).xyz, 0.0f);
  160. }
  161. ENDCG
  162. }
  163. }
  164. Fallback off
  165. }