]> git.sesse.net Git - nageru/blob - blend.frag
Support rendering forward and backward flow in parallel.
[nageru] / blend.frag
1 #version 450 core
2
3 in vec3 tc;
4 out vec4 rgba;
5
6 uniform sampler2DArray image_tex;
7 uniform sampler2D flow_tex;
8 uniform float alpha;
9
10 void main()
11 {
12         vec2 flow = texture(flow_tex, tc.xy).xy;
13         vec4 I_0 = texture(image_tex, vec3(tc.xy - alpha * flow, 0));
14         vec4 I_1 = texture(image_tex, vec3(tc.xy + (1.0f - alpha) * flow, 1));
15
16         // Occlusion reasoning:
17
18         vec2 size = textureSize(image_tex, 0).xy;
19
20         // Follow the flow back to the initial point (where we sample I_0 from), then forward again.
21         // See how well we match the point we started at, which is out flow consistency.
22         float d0 = alpha * length(size * (texture(flow_tex, vec2(tc.xy - alpha * flow)).xy - flow));
23
24         // Same for d1.
25         float d1 = (1.0f - alpha) * length(size * (texture(flow_tex, vec2(tc.xy + (1.0f - alpha) * flow)).xy - flow));
26
27         if (max(d0, d1) < 3.0f) {  // Arbitrary constant, not all that tuned. The UW paper says 1.0 is fine for ground truth.
28                 // Both are visible, so blend.
29                 rgba = I_0 + alpha * (I_1 - I_0);
30         } else if (d0 < d1) {
31                 rgba = I_0;
32         } else {
33                 rgba = I_1;
34         }
35
36 }