forked from bituq/libcargc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cBloom.fx
264 lines (220 loc) · 11.6 KB
/
cBloom.fx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
uniform float _Threshold <
ui_type = "drag";
ui_min = 0.0;
ui_label = "Threshold";
> = 0.8;
uniform float _Smooth <
ui_type = "drag";
ui_min = 0.0;
ui_label = "Smoothing";
ui_max = 1.0;
> = 0.5;
uniform float _Saturation <
ui_type = "drag";
ui_min = 0.0;
ui_label = "Saturation";
> = 1.0;
uniform float _Intensity <
ui_type = "drag";
ui_min = 0.0;
ui_label = "Intensity";
> = 1.0;
texture2D _RenderColor : COLOR;
sampler2D _SampleColor { Texture = _RenderColor; SRGBTexture = TRUE; };
texture2D _RenderBloom1 { Width = BUFFER_WIDTH / 2; Height = BUFFER_HEIGHT / 2; Format = RGBA16F; };
sampler2D _SampleBloom1 { Texture = _RenderBloom1; };
texture2D _RenderBloom2 { Width = BUFFER_WIDTH / 4; Height = BUFFER_HEIGHT / 4; Format = RGBA16F; };
sampler2D _SampleBloom2 { Texture = _RenderBloom2; };
texture2D _RenderBloom3 { Width = BUFFER_WIDTH / 8; Height = BUFFER_HEIGHT / 8; Format = RGBA16F; };
sampler2D _SampleBloom3 { Texture = _RenderBloom3; };
texture2D _RenderBloom4 { Width = BUFFER_WIDTH / 16; Height = BUFFER_HEIGHT / 16; Format = RGBA16F; };
sampler2D _SampleBloom4 { Texture = _RenderBloom4; };
texture2D _RenderBloom5 { Width = BUFFER_WIDTH / 32; Height = BUFFER_HEIGHT / 32; Format = RGBA16F; };
sampler2D _SampleBloom5 { Texture = _RenderBloom5; };
texture2D _RenderBloom6 { Width = BUFFER_WIDTH / 64; Height = BUFFER_HEIGHT / 64; Format = RGBA16F; };
sampler2D _SampleBloom6 { Texture = _RenderBloom6; };
texture2D _RenderBloom7 { Width = BUFFER_WIDTH / 128; Height = BUFFER_HEIGHT / 128; Format = RGBA16F; };
sampler2D _SampleBloom7 { Texture = _RenderBloom7; };
texture2D _RenderBloom8 { Width = BUFFER_WIDTH / 256; Height = BUFFER_HEIGHT / 256; Format = RGBA16F; };
sampler2D _SampleBloom8 { Texture = _RenderBloom8; };
/*
[ Vertex Shaders ]
Dual Filtering Algorithm
[http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare] [MIT]
*/
#define VSINPUT in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION
void PostProcessVS(VSINPUT, inout float2 TexCoord : TEXCOORD0)
{
TexCoord.x = (ID == 2) ? 2.0 : 0.0;
TexCoord.y = (ID == 1) ? 2.0 : 0.0;
Position = float4(TexCoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}
void DownsampleVS(VSINPUT, inout float4 TexCoord[4] : TEXCOORD0, float Factor)
{
float2 TexCoord0;
PostProcessVS(ID, Position, TexCoord0);
const float2 pSize = 1.0 / ldexp(float2(BUFFER_WIDTH, BUFFER_HEIGHT), -Factor);
TexCoord[0] = TexCoord0.xyxy + float4(-1.0, -1.0, 1.0, 1.0) * pSize.xyxy; // Quad
TexCoord[1] = TexCoord0.xyyy + float4(-2.0, 2.0, 0.0, -2.0) * pSize.xyyy; // Left column
TexCoord[2] = TexCoord0.xyyy + float4(0.0, 2.0, 0.0, -2.0) * pSize.xyyy; // Center column
TexCoord[3] = TexCoord0.xyyy + float4(2.0, 2.0, 0.0, -2.0) * pSize.xyyy; // Right column
}
void UpsampleVS(VSINPUT, inout float4 TexCoord[3] : TEXCOORD0, float Factor)
{
float2 TexCoord0;
PostProcessVS(ID, Position, TexCoord0);
const float2 pSize = 1.0 / ldexp(float2(BUFFER_WIDTH, BUFFER_HEIGHT), -Factor);
TexCoord[0] = TexCoord0.xyyy + float4(-1.0, 1.0, 0.0, -1.0) * pSize.xyyy; // Left column
TexCoord[1] = TexCoord0.xyyy + float4(0.0, 1.0, 0.0, -1.0) * pSize.xyyy; // Center column
TexCoord[2] = TexCoord0.xyyy + float4(1.0, 1.0, 0.0, -1.0) * pSize.xyyy; // Right column
}
void DownsampleVS1(VSINPUT, inout float4 TexCoord[4] : TEXCOORD0) { DownsampleVS(ID, Position, TexCoord, 1.0); }
void DownsampleVS2(VSINPUT, inout float4 TexCoord[4] : TEXCOORD0) { DownsampleVS(ID, Position, TexCoord, 2.0); }
void DownsampleVS3(VSINPUT, inout float4 TexCoord[4] : TEXCOORD0) { DownsampleVS(ID, Position, TexCoord, 3.0); }
void DownsampleVS4(VSINPUT, inout float4 TexCoord[4] : TEXCOORD0) { DownsampleVS(ID, Position, TexCoord, 4.0); }
void DownsampleVS5(VSINPUT, inout float4 TexCoord[4] : TEXCOORD0) { DownsampleVS(ID, Position, TexCoord, 5.0); }
void DownsampleVS6(VSINPUT, inout float4 TexCoord[4] : TEXCOORD0) { DownsampleVS(ID, Position, TexCoord, 6.0); }
void DownsampleVS7(VSINPUT, inout float4 TexCoord[4] : TEXCOORD0) { DownsampleVS(ID, Position, TexCoord, 7.0); }
void UpsampleVS8(VSINPUT, inout float4 TexCoord[3] : TEXCOORD0) { UpsampleVS(ID, Position, TexCoord, 8.0); }
void UpsampleVS7(VSINPUT, inout float4 TexCoord[3] : TEXCOORD0) { UpsampleVS(ID, Position, TexCoord, 7.0); }
void UpsampleVS6(VSINPUT, inout float4 TexCoord[3] : TEXCOORD0) { UpsampleVS(ID, Position, TexCoord, 6.0); }
void UpsampleVS5(VSINPUT, inout float4 TexCoord[3] : TEXCOORD0) { UpsampleVS(ID, Position, TexCoord, 5.0); }
void UpsampleVS4(VSINPUT, inout float4 TexCoord[3] : TEXCOORD0) { UpsampleVS(ID, Position, TexCoord, 4.0); }
void UpsampleVS3(VSINPUT, inout float4 TexCoord[3] : TEXCOORD0) { UpsampleVS(ID, Position, TexCoord, 3.0); }
void UpsampleVS2(VSINPUT, inout float4 TexCoord[3] : TEXCOORD0) { UpsampleVS(ID, Position, TexCoord, 2.0); }
/*
[ Pixel Shaders ]
Thresholding - [https://github.com/keijiro/Kino] [MIT]
Tonemap - [https://github.com/TheRealMJP/BakingLab] [MIT]
*/
float4 DownsamplePS(sampler2D Source, float4 TexCoord[4])
{
/*
A0 B0 C0
D0 D1
A1 B1 C1
D2 D3
A2 B2 C2
*/
float4 D0 = tex2D(Source, TexCoord[0].xw);
float4 D1 = tex2D(Source, TexCoord[0].zw);
float4 D2 = tex2D(Source, TexCoord[0].xy);
float4 D3 = tex2D(Source, TexCoord[0].zy);
float4 A0 = tex2D(Source, TexCoord[1].xy);
float4 A1 = tex2D(Source, TexCoord[1].xz);
float4 A2 = tex2D(Source, TexCoord[1].xw);
float4 B0 = tex2D(Source, TexCoord[2].xy);
float4 B1 = tex2D(Source, TexCoord[2].xz);
float4 B2 = tex2D(Source, TexCoord[2].xw);
float4 C0 = tex2D(Source, TexCoord[3].xy);
float4 C1 = tex2D(Source, TexCoord[3].xz);
float4 C2 = tex2D(Source, TexCoord[3].xw);
float4 Output;
const float2 Weights = float2(0.5, 0.125) / 4.0;
Output += (D0 + D1 + D2 + D3) * Weights.x;
Output += (A0 + B0 + A1 + B1) * Weights.y;
Output += (B0 + C0 + B1 + C1) * Weights.y;
Output += (A1 + B1 + A2 + B2) * Weights.y;
Output += (B1 + C1 + B2 + C2) * Weights.y;
return float4(Output.rgb, 1.0);
}
float4 UpsamplePS(sampler2D Source, float4 TexCoord[3])
{
/*
A0 B0 C0
A1 B1 C1
A2 B2 C2
*/
float4 A0 = tex2D(Source, TexCoord[0].xy);
float4 A1 = tex2D(Source, TexCoord[0].xz);
float4 A2 = tex2D(Source, TexCoord[0].xw);
float4 B0 = tex2D(Source, TexCoord[1].xy);
float4 B1 = tex2D(Source, TexCoord[1].xz);
float4 B2 = tex2D(Source, TexCoord[1].xw);
float4 C0 = tex2D(Source, TexCoord[2].xy);
float4 C1 = tex2D(Source, TexCoord[2].xz);
float4 C2 = tex2D(Source, TexCoord[2].xw);
float4 Output;
Output = (A0 + C0 + A2 + C2) * 1.0;
Output += (A1 + B0 + C1 + B2) * 2.0;
Output += B1 * 4.0;
return Output / 16.0;
}
void PrefilterPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
const float Knee = mad(_Threshold, _Smooth, 1e-5f);
const float3 Curve = float3(_Threshold - Knee, Knee * 2.0, 0.25 / Knee);
float4 Color = tex2D(_SampleColor, TexCoord);
// Under-threshold
float Brightness = max(Color.r, max(Color.g, Color.b));
float ResponseCurve = clamp(Brightness - Curve.x, 0.0, Curve.y);
ResponseCurve = Curve.z * ResponseCurve * ResponseCurve;
// Combine and apply the brightness response curve
Color = Color * max(ResponseCurve, Brightness - _Threshold) / max(Brightness, 1e-10);
Brightness = max(max(Color.r, Color.g), Color.b);
OutputColor0 = saturate(lerp(Brightness, Color.rgb, _Saturation));
OutputColor0.a = 1.0;
}
// sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
static const float3x3 ACESInputMat = float3x3(
0.59719, 0.35458, 0.04823,
0.07600, 0.90834, 0.01566,
0.02840, 0.13383, 0.83777
);
// ODT_SAT => XYZ => D60_2_D65 => sRGB
static const float3x3 ACESOutputMat = float3x3(
1.60475, -0.53108, -0.07367,
-0.10208, 1.10813, -0.00605,
-0.00327, -0.07276, 1.07602
);
float3 RRTAndODTFit(float3 v)
{
float3 a = v * (v + 0.0245786f) - 0.000090537f;
float3 b = v * (0.983729f * v + 0.4329510f) + 0.238081f;
return a / b;
}
#define PSINPUT(i) float4 Position : SV_POSITION, float4 TexCoord[i] : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0
void DownsamplePS1(PSINPUT(4)) { OutputColor0 = DownsamplePS(_SampleBloom1, TexCoord); }
void DownsamplePS2(PSINPUT(4)) { OutputColor0 = DownsamplePS(_SampleBloom2, TexCoord); }
void DownsamplePS3(PSINPUT(4)) { OutputColor0 = DownsamplePS(_SampleBloom3, TexCoord); }
void DownsamplePS4(PSINPUT(4)) { OutputColor0 = DownsamplePS(_SampleBloom4, TexCoord); }
void DownsamplePS5(PSINPUT(4)) { OutputColor0 = DownsamplePS(_SampleBloom5, TexCoord); }
void DownsamplePS6(PSINPUT(4)) { OutputColor0 = DownsamplePS(_SampleBloom6, TexCoord); }
void DownsamplePS7(PSINPUT(4)) { OutputColor0 = DownsamplePS(_SampleBloom7, TexCoord); }
void UpsamplePS8(PSINPUT(3)) { OutputColor0 = UpsamplePS(_SampleBloom8, TexCoord); }
void UpsamplePS7(PSINPUT(3)) { OutputColor0 = UpsamplePS(_SampleBloom7, TexCoord); }
void UpsamplePS6(PSINPUT(3)) { OutputColor0 = UpsamplePS(_SampleBloom6, TexCoord); }
void UpsamplePS5(PSINPUT(3)) { OutputColor0 = UpsamplePS(_SampleBloom5, TexCoord); }
void UpsamplePS4(PSINPUT(3)) { OutputColor0 = UpsamplePS(_SampleBloom4, TexCoord); }
void UpsamplePS3(PSINPUT(3)) { OutputColor0 = UpsamplePS(_SampleBloom3, TexCoord); }
void UpsamplePS2(PSINPUT(3)) { OutputColor0 = UpsamplePS(_SampleBloom2, TexCoord); }
void CompositePS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
float4 Src = tex2D(_SampleBloom1, TexCoord);
Src *= _Intensity;
Src = mul(ACESInputMat, Src.rgb);
Src = RRTAndODTFit(Src.rgb);
Src = saturate(mul(ACESOutputMat, Src.rgb));
OutputColor0 = Src;
}
/* [ TECHNIQUE ] */
technique cBloom
{
#define BLEND(i, j, k) BlendEnable = TRUE; BlendOp = i; SrcBlend = j; DestBlend = k
pass { VertexShader = PostProcessVS; PixelShader = PrefilterPS; RenderTarget = _RenderBloom1; }
pass { VertexShader = DownsampleVS1; PixelShader = DownsamplePS1; RenderTarget = _RenderBloom2; }
pass { VertexShader = DownsampleVS2; PixelShader = DownsamplePS2; RenderTarget = _RenderBloom3; }
pass { VertexShader = DownsampleVS3; PixelShader = DownsamplePS3; RenderTarget = _RenderBloom4; }
pass { VertexShader = DownsampleVS4; PixelShader = DownsamplePS4; RenderTarget = _RenderBloom5; }
pass { VertexShader = DownsampleVS5; PixelShader = DownsamplePS5; RenderTarget = _RenderBloom6; }
pass { VertexShader = DownsampleVS6; PixelShader = DownsamplePS6; RenderTarget = _RenderBloom7; }
pass { VertexShader = DownsampleVS7; PixelShader = DownsamplePS7; RenderTarget = _RenderBloom8; }
pass { VertexShader = UpsampleVS8; PixelShader = UpsamplePS8; RenderTarget = _RenderBloom7; BLEND(ADD, ONE, ONE); }
pass { VertexShader = UpsampleVS7; PixelShader = UpsamplePS7; RenderTarget = _RenderBloom6; BLEND(ADD, ONE, ONE); }
pass { VertexShader = UpsampleVS6; PixelShader = UpsamplePS6; RenderTarget = _RenderBloom5; BLEND(ADD, ONE, ONE); }
pass { VertexShader = UpsampleVS5; PixelShader = UpsamplePS5; RenderTarget = _RenderBloom4; BLEND(ADD, ONE, ONE); }
pass { VertexShader = UpsampleVS4; PixelShader = UpsamplePS4; RenderTarget = _RenderBloom3; BLEND(ADD, ONE, ONE); }
pass { VertexShader = UpsampleVS3; PixelShader = UpsamplePS3; RenderTarget = _RenderBloom2; BLEND(ADD, ONE, ONE); }
pass { VertexShader = UpsampleVS2; PixelShader = UpsamplePS2; RenderTarget = _RenderBloom1; }
pass { VertexShader = PostProcessVS; PixelShader = CompositePS; BLEND(ADD, ONE, INVSRCCOLOR); SRGBWriteEnable = TRUE; }
}