summaryrefslogtreecommitdiffstatshomepage
path: root/hlsl/bloom.fx
blob: 1ee152adfecd929b36e8ef0af0dd1f116ae26392 (plain) (blame)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz,ImJezze
//-----------------------------------------------------------------------------
// Bloom Effect
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Sampler Definitions
//-----------------------------------------------------------------------------

texture DiffuseA;
texture DiffuseB;
texture DiffuseC;
texture DiffuseD;
texture DiffuseE;
texture DiffuseF;
texture DiffuseG;
texture DiffuseH;
texture DiffuseI;
texture DiffuseJ;
texture DiffuseK;

sampler DiffuseSampler0 = sampler_state
{
	Texture = <DiffuseA>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler1 = sampler_state
{
	Texture = <DiffuseB>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler2 = sampler_state
{
	Texture = <DiffuseC>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler3 = sampler_state
{
	Texture = <DiffuseD>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler4 = sampler_state
{
	Texture = <DiffuseE>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler5 = sampler_state
{
	Texture = <DiffuseF>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler6 = sampler_state
{
	Texture = <DiffuseG>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler7 = sampler_state
{
	Texture = <DiffuseH>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler8 = sampler_state
{
	Texture = <DiffuseI>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSampler9 = sampler_state
{
	Texture = <DiffuseJ>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

sampler DiffuseSamplerA = sampler_state
{
	Texture = <DiffuseK>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
};

//-----------------------------------------------------------------------------
// Vertex Definitions
//-----------------------------------------------------------------------------

struct VS_OUTPUT
{
	float4 Position : POSITION;
	float4 Color : COLOR0;
	float2 TexCoord0 : TEXCOORD0;
	float4 TexCoord12 : TEXCOORD1;
	float4 TexCoord34 : TEXCOORD2;
	float4 TexCoord56 : TEXCOORD3;
	float4 TexCoord78 : TEXCOORD4;
	float4 TexCoord9A : TEXCOORD5;
};

struct VS_INPUT
{
	float4 Position : POSITION;
	float4 Color : COLOR0;
	float2 TexCoord : TEXCOORD0;
};

struct PS_INPUT
{
	float4 Color : COLOR0;
	float2 TexCoord0 : TEXCOORD0;
	float4 TexCoord12 : TEXCOORD1;
	float4 TexCoord34 : TEXCOORD2;
	float4 TexCoord56 : TEXCOORD3;
	float4 TexCoord78 : TEXCOORD4;
	float4 TexCoord9A : TEXCOORD5;
};

//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------

static const float E = 2.7182817f;
static const float Gelfond = 23.140692f; // e^pi (Gelfond constant)
static const float GelfondSchneider = 2.6651442f; // 2^sqrt(2) (Gelfond-Schneider constant)

//-----------------------------------------------------------------------------
// Funcions
//-----------------------------------------------------------------------------

// www.stackoverflow.com/questions/5149544/can-i-generate-a-random-number-inside-a-pixel-shader/
float random(float2 seed)
{
	// irrationals for pseudo randomness
	float2 i = float2(Gelfond, GelfondSchneider);

	return frac(cos(dot(seed, i)) * 123456.0f);
}

//-----------------------------------------------------------------------------
// Bloom Vertex Shader
//-----------------------------------------------------------------------------

uniform float2 ScreenDims;
uniform float2 TargetDims;
uniform float2 SourceRect;

uniform float2 Level0Size;
uniform float4 Level12Size;
uniform float4 Level34Size;
uniform float4 Level56Size;
uniform float4 Level78Size;
uniform float4 Level9ASize;

VS_OUTPUT vs_main(VS_INPUT Input)
{
	VS_OUTPUT Output = (VS_OUTPUT)0;

	Output.Position = float4(Input.Position.xyz, 1.0f);
	Output.Position.xy /= ScreenDims;
	Output.Position.y = 1.0f - Output.Position.y; // flip y
	Output.Position.xy -= 0.5f; // center
	Output.Position.xy *= 2.0f; // zoom

	Output.Color = Input.Color;

	float2 TexCoord = Input.Position.xy / ScreenDims;
	TexCoord += 0.5f / TargetDims; // half texel offset correction (DX9)

	Output.TexCoord0 = TexCoord;
	Output.TexCoord12 = TexCoord.xyxy + (0.5f / Level12Size);
	Output.TexCoord34 = TexCoord.xyxy + (0.5f / Level34Size);
	Output.TexCoord56 = TexCoord.xyxy + (0.5f / Level56Size);
	Output.TexCoord78 = TexCoord.xyxy + (0.5f / Level78Size);
	Output.TexCoord9A = TexCoord.xyxy + (0.5f / Level9ASize);

	return Output;
}

//-----------------------------------------------------------------------------
// Bloom Pixel Shader
//-----------------------------------------------------------------------------

uniform float Level0Weight;
uniform float2 Level12Weight;
uniform float2 Level34Weight;
uniform float2 Level56Weight;
uniform float2 Level78Weight;
uniform float2 Level9AWeight;

uniform int BloomBlendMode = 0; // 0 addition, 1 darken
uniform float BloomScale;
uniform float3 BloomOverdrive;

float3 GetNoiseFactor(float3 n, float random)
{
	// smaller n become more noisy
	return 1.0f + random * max(0.0f, 0.25f * pow(E, -8 * n));
}

float4 ps_main(PS_INPUT Input) : COLOR
{
	float3 texel0 = tex2D(DiffuseSampler0, Input.TexCoord0).rgb;
	float3 texel1 = tex2D(DiffuseSampler1, Input.TexCoord12.xy).rgb;
	float3 texel2 = tex2D(DiffuseSampler2, Input.TexCoord12.zw).rgb;
	float3 texel3 = tex2D(DiffuseSampler3, Input.TexCoord34.xy).rgb;
	float3 texel4 = tex2D(DiffuseSampler4, Input.TexCoord34.zw).rgb;
	float3 texel5 = tex2D(DiffuseSampler5, Input.TexCoord56.xy).rgb;
	float3 texel6 = tex2D(DiffuseSampler6, Input.TexCoord56.zw).rgb;
	float3 texel7 = tex2D(DiffuseSampler7, Input.TexCoord78.xy).rgb;
	float3 texel8 = tex2D(DiffuseSampler8, Input.TexCoord78.zw).rgb;
	float3 texel9 = tex2D(DiffuseSampler9, Input.TexCoord9A.xy).rgb;
	float3 texelA = tex2D(DiffuseSamplerA, Input.TexCoord9A.zw).rgb;

	float3 blend;

	// addition
	if (BloomBlendMode == 0)
	{
		texel0 *= Level0Weight;
		texel1 *= Level12Weight.x;
		texel2 *= Level12Weight.y;
		texel3 *= Level34Weight.x;
		texel4 *= Level34Weight.y;
		texel5 *= Level56Weight.x;
		texel6 *= Level56Weight.y;
		texel7 *= Level78Weight.x;
		texel8 *= Level78Weight.y;
		texel9 *= Level9AWeight.x;
		texelA *= Level9AWeight.y;

		float3 bloom = float3(
			texel1 +
			texel2 +
			texel3 +
			texel4 +
			texel5 +
			texel6 +
			texel7 +
			texel8 +
			texel9 +
			texelA) * BloomScale;

		float3 bloomOverdrive = max(0.0f, texel0 + bloom - 1.0f) * BloomOverdrive;

		bloom.r += bloomOverdrive.g * 0.5f;
		bloom.r += bloomOverdrive.b * 0.5f;
		bloom.g += bloomOverdrive.r * 0.5f;
		bloom.g += bloomOverdrive.b * 0.5f;
		bloom.b += bloomOverdrive.r * 0.5f;
		bloom.b += bloomOverdrive.g * 0.5f;

		float2 NoiseCoord = Input.TexCoord0;
		float3 NoiseFactor = GetNoiseFactor(bloom, random(NoiseCoord));

		blend = texel0 + bloom * NoiseFactor;
	}

	// darken
	else
	{
		texel1 = min(texel0, texel1);
		texel2 = min(texel0, texel2);
		texel3 = min(texel0, texel3);
		texel4 = min(texel0, texel4);
		texel5 = min(texel0, texel5);
		texel6 = min(texel0, texel6);
		texel7 = min(texel0, texel7);
		texel8 = min(texel0, texel8);
		texel9 = min(texel0, texel9);
		texelA = min(texel0, texelA);

		blend = texel0 * Level0Weight;
		blend = lerp(blend, texel1, Level12Weight.x * BloomScale);
		blend = lerp(blend, texel2, Level12Weight.y * BloomScale);
		blend = lerp(blend, texel3, Level34Weight.x * BloomScale);
		blend = lerp(blend, texel4, Level34Weight.y * BloomScale);
		blend = lerp(blend, texel5, Level56Weight.x * BloomScale);
		blend = lerp(blend, texel6, Level56Weight.y * BloomScale);
		blend = lerp(blend, texel7, Level78Weight.x * BloomScale);
		blend = lerp(blend, texel8, Level78Weight.y * BloomScale);
		blend = lerp(blend, texel9, Level9AWeight.x * BloomScale);
		blend = lerp(blend, texelA, Level9AWeight.y * BloomScale);
	}

	return float4(blend, 1.0f);
}

//-----------------------------------------------------------------------------
// Bloom Technique
//-----------------------------------------------------------------------------

technique DefaultTechnique
{
	pass Pass0
	{
		Lighting = FALSE;

		VertexShader = compile vs_3_0 vs_main();
		PixelShader = compile ps_3_0 ps_main();
	}
}