-
Notifications
You must be signed in to change notification settings - Fork 2k
/
opengl_fragment.glsl
157 lines (130 loc) · 3.78 KB
/
opengl_fragment.glsl
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
#define rendered texture0
#define bloom texture1
#ifdef GL_ES
// Dithering requires sufficient floating-point precision
#ifndef GL_FRAGMENT_PRECISION_HIGH
#undef ENABLE_DITHERING
#endif
#endif
struct ExposureParams {
float compensationFactor;
};
uniform sampler2D rendered;
uniform sampler2D bloom;
uniform vec2 texelSize0;
uniform ExposureParams exposureParams;
uniform lowp float bloomIntensity;
uniform lowp float saturation;
#ifdef GL_ES
varying mediump vec2 varTexCoord;
#else
centroid varying vec2 varTexCoord;
#endif
#ifdef ENABLE_AUTO_EXPOSURE
varying float exposure; // linear exposure factor, see vertex shader
#endif
#ifdef ENABLE_BLOOM
vec4 applyBloom(vec4 color, vec2 uv)
{
vec3 light = texture2D(bloom, uv).rgb;
#ifdef ENABLE_BLOOM_DEBUG
if (uv.x > 0.5 && uv.y < 0.5)
return vec4(light, color.a);
if (uv.x < 0.5)
return color;
#endif
color.rgb = mix(color.rgb, light, bloomIntensity);
return color;
}
#endif
#if ENABLE_TONE_MAPPING
/* Hable's UC2 Tone mapping parameters
A = 0.22;
B = 0.30;
C = 0.10;
D = 0.20;
E = 0.01;
F = 0.30;
W = 11.2;
equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
*/
vec3 uncharted2Tonemap(vec3 x)
{
return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
}
vec4 applyToneMapping(vec4 color)
{
color = vec4(pow(color.rgb, vec3(2.2)), color.a);
const float gamma = 1.6;
const float exposureBias = 5.5;
color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
// Precalculated white_scale from
//vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
vec3 whiteScale = vec3(1.036015346);
color.rgb *= whiteScale;
return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
}
vec3 applySaturation(vec3 color, float factor)
{
// Calculate the perceived luminosity from the RGB color.
// See also: https://www.w3.org/WAI/GL/wiki/Relative_luminance
float brightness = dot(color, vec3(0.2125, 0.7154, 0.0721));
return mix(vec3(brightness), color, factor);
}
#endif
#ifdef ENABLE_DITHERING
// From http://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
// and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
// NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
vec3 screen_space_dither(highp vec2 frag_coord) {
// Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
highp vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
// Subtract 0.5 to avoid slightly brightening the whole viewport.
return (dither.rgb - 0.5) / 255.0;
}
#endif
void main(void)
{
vec2 uv = varTexCoord.st;
#ifdef ENABLE_SSAA
vec4 color = vec4(0.);
for (float dx = 1.; dx < SSAA_SCALE; dx += 2.)
for (float dy = 1.; dy < SSAA_SCALE; dy += 2.)
color += texture2D(rendered, uv + texelSize0 * vec2(dx, dy)).rgba;
color /= SSAA_SCALE * SSAA_SCALE / 4.;
#else
vec4 color = texture2D(rendered, uv).rgba;
#endif
// translate to linear colorspace (approximate)
color.rgb = pow(color.rgb, vec3(2.2));
#ifdef ENABLE_BLOOM_DEBUG
if (uv.x > 0.5 || uv.y > 0.5)
#endif
{
color.rgb *= exposureParams.compensationFactor;
#ifdef ENABLE_AUTO_EXPOSURE
color.rgb *= exposure;
#endif
}
#ifdef ENABLE_BLOOM
color = applyBloom(color, uv);
#endif
color.rgb = clamp(color.rgb, vec3(0.), vec3(1.));
// return to sRGB colorspace (approximate)
color.rgb = pow(color.rgb, vec3(1.0 / 2.2));
#ifdef ENABLE_BLOOM_DEBUG
if (uv.x > 0.5 || uv.y > 0.5)
#endif
{
#if ENABLE_TONE_MAPPING
color = applyToneMapping(color);
color.rgb = applySaturation(color.rgb, saturation);
#endif
}
#ifdef ENABLE_DITHERING
// Apply dithering just before quantisation
color.rgb += screen_space_dither(gl_FragCoord.xy);
#endif
gl_FragColor = vec4(color.rgb, 1.0); // force full alpha to avoid holes in the image.
}