summaryrefslogtreecommitdiff
path: root/intermediate/tonemap.glsl
blob: 5c41a6eeeee595eb6d3eabc651339b47ef2c4549 (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
#ifdef DESC
[program]
type: graphics
vertex: main
fragment: main

[binding]
name: verts
rate: vertex
[attribute]
name: position
type: vec2
[attribute]
name: uv
type: vec2

[interpolator]
name: uv
type: vec2

[struct]
name: Config
[variable]
name: exposure
type: float

[cbuffer]
name: config
type: Config
stage: fragment

[texture]
name: src
stage: fragment
dimension: 2

[target]
name: colour
type: vec4
#endif

#ifdef VERTEX_SHADER
void main() {
	interpolator.uv = uv;
	gl_Position = vec4(position, 1.0, 1.0);
}
#endif

#ifdef FRAGMENT_SHADER

vec3 to_srgb(vec3 lin) {
	bvec3 cutoff = lessThan(lin, 0.0031308.xxx);
	vec3 higher = 1.055.xxx * pow(lin, vec3(1.0 / 2.4)) - 0.055.xxx;
	vec3 lower = lin.rgb * 12.92.xxx;
	return mix(higher, lower, cutoff);
}

vec3 aces(vec3 x) {
	float a = 2.51;
	float b = 0.03;
	float c = 2.43;
	float d = 0.59;
	float e = 0.14;
	return clamp(
		(x * (a * x + b)) / (x * (c * x + d) + e),
		0.0,
		1.0f
	);
}

vec3 hejl(vec3 x, float whitepoint) {
	vec4 vh = vec4(x, whitepoint);
	vec4 va = (1.425 * vh) + 0.05;
	vec4 vf = ((vh * va + 0.004f) / ((vh * (va + 0.55f) + 0.0491f))) - 0.0821f;
	return vf.rgb / vf.www;
}

void main() {
	vec2 uv = interpolator.uv;
	vec3 lin = texture(src, uv).rgb * config.exposure;
	lin = hejl(lin, 1.0);
	colour = vec4(to_srgb(lin), 1.0);
}
#endif