]> git.sesse.net Git - nageru/blob - densify.vert
Use textureSize() instead of sending in uniforms manually. Same result, less code...
[nageru] / densify.vert
1 #version 450 core
2
3 in vec2 position;
4 out vec2 image_pos;
5 flat out vec2 flow_du;
6 flat out float mean_diff;
7
8 uniform vec2 patch_size;  // In 0..1 coordinates.
9 uniform vec2 patch_spacing;  // In 0..1 coordinates.
10 uniform sampler2D flow_tex;
11
12 void main()
13 {
14         int patch_x = gl_InstanceID % textureSize(flow_tex, 0).x;
15         int patch_y = gl_InstanceID / textureSize(flow_tex, 0).x;
16
17         // Convert the patch index to being the full 0..1 range, to match where
18         // the motion search puts the patches. We don't bother with the locking
19         // to texel centers, though.
20         vec2 patch_center = ivec2(patch_x, patch_y) / (textureSize(flow_tex, 0) - 1.0);
21
22         // Increase the patch size a bit; since patch spacing is not necessarily
23         // an integer number of pixels, and we don't use conservative rasterization,
24         // we could be missing the outer edges of the patch. And it seemingly helps
25         // a little bit in general to have some more candidates as well -- although
26         // this is measured without variational refinement, so it might be moot
27         // with it.
28         //
29         // This maps [0.0,1.0] to [-0.25,1.25], ie. extends the patch by 25% in
30         // all directions.
31         vec2 grown_pos = (position * 1.5) - 0.25;
32
33         image_pos = patch_center + patch_size * (grown_pos - 0.5f);
34
35         // Find the flow value for this patch, and send it on to the fragment shader.
36         vec3 flow_du_and_mean_diff = texelFetch(flow_tex, ivec2(patch_x, patch_y), 0).xyz;
37         flow_du = flow_du_and_mean_diff.xy;
38         mean_diff = flow_du_and_mean_diff.z;
39
40         // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is:
41         //
42         //   2.000  0.000  0.000 -1.000
43         //   0.000  2.000  0.000 -1.000
44         //   0.000  0.000 -2.000 -1.000
45         //   0.000  0.000  0.000  1.000
46         gl_Position = vec4(2.0 * image_pos.x - 1.0, 2.0 * image_pos.y - 1.0, -1.0, 1.0);
47 }