Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/motion_vectors.glsl

raw
1 #[vertex]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 layout(location = 0) out vec2 uv_interp;
8
9 void main() {
10 vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
11 gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
12 uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
13 }
14
15 #[fragment]
16
17 #version 450
18
19 #VERSION_DEFINES
20
21 #include "motion_vector_inc.glsl"
22
23 layout(location = 0) in vec2 uv_interp;
24
25 layout(set = 0, binding = 0) uniform sampler2D source_velocity;
26 layout(set = 0, binding = 1) uniform sampler2D source_depth;
27
28 layout(location = 0) out vec4 frag_color;
29
30 layout(push_constant, std430) uniform Params {
31 highp mat4 reprojection_matrix;
32 vec2 resolution;
33 bool force_derive_from_depth;
34 }
35 params;
36
37 // Based on distance to line segment from https://www.shadertoy.com/view/3tdSDj
38
39 float line_segment(in vec2 p, in vec2 a, in vec2 b) {
40 vec2 aspect = vec2(params.resolution.x / params.resolution.y, 1.0f);
41 vec2 ba = (b - a) * aspect;
42 vec2 pa = (p - a) * aspect;
43 float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0f, 1.0f);
44 return length(pa - h * ba) * (params.resolution.y / 2.0f);
45 }
46
47 void main() {
48 // Retrieve motion vector data.
49 float cell_size = 32.0f;
50 float circle_radius = 2.0f;
51 vec3 nan_color = vec3(1.0f, 0.0f, 0.0f);
52 vec3 active_color = vec3(1.0f, 0.8f, 0.1f);
53 vec3 inactive_color = vec3(0.5f, 0.5f, 0.5f);
54 vec2 pos_pixel = uv_interp * params.resolution;
55 vec2 cell_pos_pixel = floor(pos_pixel / cell_size) * cell_size + (cell_size * 0.5f);
56 vec2 cell_pos_uv = cell_pos_pixel / params.resolution;
57 vec2 cell_pos_velocity = textureLod(source_velocity, cell_pos_uv, 0.0f).xy;
58 bool derive_velocity = params.force_derive_from_depth || all(lessThanEqual(cell_pos_velocity, vec2(-1.0f, -1.0f)));
59 if (derive_velocity) {
60 float depth = textureLod(source_depth, cell_pos_uv, 0.0f).x;
61 cell_pos_velocity = derive_motion_vector(cell_pos_uv, depth, params.reprojection_matrix);
62 }
63
64 vec2 cell_pos_previous_uv = cell_pos_uv + cell_pos_velocity;
65
66 // Draw the shapes.
67 float epsilon = 1e-6f;
68 vec2 cell_pos_delta_uv = cell_pos_uv - cell_pos_previous_uv;
69 bool motion_active = length(cell_pos_delta_uv) > epsilon;
70 vec3 color;
71 if (any(isnan(cell_pos_delta_uv))) {
72 color = nan_color;
73 } else if (motion_active) {
74 color = active_color;
75 } else {
76 color = inactive_color;
77 }
78
79 float alpha;
80 if (length(cell_pos_pixel - pos_pixel) <= circle_radius) {
81 // Circle center.
82 alpha = 1.0f;
83 } else if (motion_active) {
84 // Motion vector line.
85 alpha = 1.0f - line_segment(uv_interp, cell_pos_uv, cell_pos_previous_uv);
86 } else {
87 // Ignore pixel.
88 alpha = 0.0f;
89 }
90
91 if (derive_velocity) {
92 color = vec3(1.0f, 1.0f, 1.0f) - color;
93 alpha *= 0.5f;
94 }
95
96 frag_color = vec4(color, alpha);
97 }