summaryrefslogtreecommitdiff
path: root/drivers/gles3/shaders/exposure.glsl
blob: 759adcda06a8e64206600e69b7c2ca65340dc695 (plain)
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
/* clang-format off */
[vertex]

layout(location = 0) in highp vec4 vertex_attrib;
/* clang-format on */

void main() {

	gl_Position = vertex_attrib;
}

/* clang-format off */
[fragment]

uniform highp sampler2D source_exposure; //texunit:0
/* clang-format on */

#ifdef EXPOSURE_BEGIN

uniform highp ivec2 source_render_size;
uniform highp ivec2 target_size;

#endif

#ifdef EXPOSURE_END

uniform highp sampler2D prev_exposure; //texunit:1
uniform highp float exposure_adjust;
uniform highp float min_luminance;
uniform highp float max_luminance;

#endif

layout(location = 0) out highp float exposure;

void main() {

#ifdef EXPOSURE_BEGIN

	ivec2 src_pos = ivec2(gl_FragCoord.xy) * source_render_size / target_size;

#if 1
	//more precise and expensive, but less jittery
	ivec2 next_pos = ivec2(gl_FragCoord.xy + ivec2(1)) * source_render_size / target_size;
	next_pos = max(next_pos, src_pos + ivec2(1)); //so it at least reads one pixel
	highp vec3 source_color = vec3(0.0);
	for (int i = src_pos.x; i < next_pos.x; i++) {
		for (int j = src_pos.y; j < next_pos.y; j++) {
			source_color += texelFetch(source_exposure, ivec2(i, j), 0).rgb;
		}
	}

	source_color /= float((next_pos.x - src_pos.x) * (next_pos.y - src_pos.y));
#else
	highp vec3 source_color = texelFetch(source_exposure, src_pos, 0).rgb;

#endif

	exposure = max(source_color.r, max(source_color.g, source_color.b));

#else

	ivec2 coord = ivec2(gl_FragCoord.xy);
	exposure = texelFetch(source_exposure, coord * 3 + ivec2(0, 0), 0).r;
	exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 0), 0).r;
	exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 0), 0).r;
	exposure += texelFetch(source_exposure, coord * 3 + ivec2(0, 1), 0).r;
	exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 1), 0).r;
	exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 1), 0).r;
	exposure += texelFetch(source_exposure, coord * 3 + ivec2(0, 2), 0).r;
	exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 2), 0).r;
	exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 2), 0).r;
	exposure *= (1.0 / 9.0);

#ifdef EXPOSURE_END

#ifdef EXPOSURE_FORCE_SET
	//will stay as is
#else
	highp float prev_lum = texelFetch(prev_exposure, ivec2(0, 0), 0).r; //1 pixel previous exposure
	exposure = clamp(prev_lum + (exposure - prev_lum) * exposure_adjust, min_luminance, max_luminance);

#endif //EXPOSURE_FORCE_SET

#endif //EXPOSURE_END

#endif //EXPOSURE_BEGIN
}