summaryrefslogtreecommitdiffstats
path: root/servers/rendering/renderer_rd/shaders/screen_space_reflection_scale.glsl
blob: 3f537e273a6164691a225464ed60e0a2de401d36 (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
89
90
#[compute]

#version 450

#VERSION_DEFINES

layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

layout(set = 0, binding = 0) uniform sampler2D source_ssr;
layout(set = 1, binding = 0) uniform sampler2D source_depth;
layout(set = 1, binding = 1) uniform sampler2D source_normal;
layout(rgba16f, set = 2, binding = 0) uniform restrict writeonly image2D dest_ssr;
layout(r32f, set = 3, binding = 0) uniform restrict writeonly image2D dest_depth;
layout(rgba8, set = 3, binding = 1) uniform restrict writeonly image2D dest_normal;

layout(push_constant, std430) uniform Params {
	ivec2 screen_size;
	float camera_z_near;
	float camera_z_far;

	bool orthogonal;
	bool filtered;
	uint pad[2];
}
params;

void main() {
	// Pixel being shaded
	ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);

	if (any(greaterThanEqual(ssC, params.screen_size))) { //too large, do nothing
		return;
	}
	//do not filter, SSR will generate arctifacts if this is done

	float divisor = 0.0;
	vec4 color;
	float depth;
	vec4 normal;

	if (params.filtered) {
		color = vec4(0.0);
		depth = 0.0;
		normal = vec4(0.0);

		for (int i = 0; i < 4; i++) {
			ivec2 ofs = ssC << 1;
			if (bool(i & 1)) {
				ofs.x += 1;
			}
			if (bool(i & 2)) {
				ofs.y += 1;
			}
			color += texelFetch(source_ssr, ofs, 0);
			float d = texelFetch(source_depth, ofs, 0).r;
			vec4 nr = texelFetch(source_normal, ofs, 0);
			normal.xyz += nr.xyz * 2.0 - 1.0;
			normal.w += nr.w;

			d = d * 2.0 - 1.0;
			if (params.orthogonal) {
				d = ((d + (params.camera_z_far + params.camera_z_near) / (params.camera_z_far - params.camera_z_near)) * (params.camera_z_far - params.camera_z_near)) / 2.0;
			} else {
				d = 2.0 * params.camera_z_near * params.camera_z_far / (params.camera_z_far + params.camera_z_near - d * (params.camera_z_far - params.camera_z_near));
			}
			depth += -d;
		}

		color /= 4.0;
		depth /= 4.0;
		normal.xyz = normalize(normal.xyz / 4.0) * 0.5 + 0.5;
		normal.w /= 4.0;
	} else {
		color = texelFetch(source_ssr, ssC << 1, 0);
		depth = texelFetch(source_depth, ssC << 1, 0).r;
		normal = texelFetch(source_normal, ssC << 1, 0);

		depth = depth * 2.0 - 1.0;
		if (params.orthogonal) {
			depth = ((depth + (params.camera_z_far + params.camera_z_near) / (params.camera_z_far - params.camera_z_near)) * (params.camera_z_far - params.camera_z_near)) / 2.0;
		} else {
			depth = 2.0 * params.camera_z_near * params.camera_z_far / (params.camera_z_far + params.camera_z_near - depth * (params.camera_z_far - params.camera_z_near));
		}
		depth = -depth;
	}

	imageStore(dest_ssr, ssC, color);
	imageStore(dest_depth, ssC, vec4(depth));
	imageStore(dest_normal, ssC, normal);
}