-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscene_data_helpers.glsl
53 lines (40 loc) · 1.64 KB
/
scene_data_helpers.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
// Set 0: Scene
layout(set = 0, binding = 0, std140) uniform SceneDataBlock {
SceneData data;
SceneData prev_data;
} scene;
// Color image uniform (binding = 1) should be defined separately with restrict
// writeonly/readonly params.
layout(set = 0, binding = 2) uniform sampler2D depth_sampler;
layout(set = 0, binding = 3) uniform sampler2D normal_roughness_sampler;
// Converts coord obtained from gl_GlobalInvocationID
// to normalize [0.0-1.0] for use in texture() sampling functions.
highp vec2 coord_to_uv(ivec2 p_coord) {
return (vec2(p_coord) + 0.5)/scene.data.viewport_size;
}
// Uncompress the normal roughness texture values.
// Godot automatically applies this conversion for Spatial shaders.
// see: https://github.com/godotengine/godot-docs/issues/9591
highp vec4 unpack_normal_roughness(vec4 p_normal_roughness) {
float roughness = p_normal_roughness.w;
if (roughness > 0.5) {
roughness = 1.0 - roughness;
}
roughness /= (127.0 / 255.0);
return vec4(normalize(p_normal_roughness.xyz * 2.0 - 1.0) * 0.5 + 0.5, roughness);
}
highp vec4 get_normal_roughness_color(ivec2 p_coord) {
return unpack_normal_roughness(texelFetch(normal_roughness_sampler, p_coord, 0));
}
highp float get_raw_depth(ivec2 p_coord) {
return texelFetch(depth_sampler, p_coord, 0).r;
}
highp float raw_to_linear_depth(ivec2 p_coord, highp float p_raw_depth) {
highp vec2 uv = coord_to_uv(p_coord);
highp vec3 ndc = vec3((uv * 2.0) - 1.0, p_raw_depth);
highp vec4 view = scene.data.inv_projection_matrix * vec4(ndc, 1.0);
return -(view.xyz / view.w).z;
}
highp float get_linear_depth(ivec2 p_coord) {
return raw_to_linear_depth(p_coord, get_raw_depth(p_coord));
}