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