]> git.sesse.net Git - nageru/blob - smoothness.frag
Remove some dead code.
[nageru] / smoothness.frag
1 #version 450 core
2
3 in vec2 tc;
4 out float smoothness_x, smoothness_y;
5 const float eps_sq = 0.001 * 0.001;
6
7 uniform sampler2D flow_tex, diff_flow_tex;
8
9 // Relative weighting of smoothness term.
10 uniform float alpha;
11
12 uniform bool zero_diff_flow;
13
14 // This must be a macro, since the offset needs to be a constant expression.
15 #define get_flow(x_offs, y_offs) \
16         (textureOffset(flow_tex, tc, ivec2((x_offs), (y_offs))).xy + \
17         textureOffset(diff_flow_tex, tc, ivec2((x_offs), (y_offs))).xy)
18
19 #define get_flow_no_diff(x_offs, y_offs) \
20         textureOffset(flow_tex, tc, ivec2((x_offs), (y_offs))).xy
21
22 float diffusivity(float u_x, float u_y, float v_x, float v_y)
23 {
24         return alpha * inversesqrt(u_x * u_x + u_y * u_y + v_x * v_x + v_y * v_y + eps_sq);
25 }
26
27 void main()
28 {
29         float g, g_right, g_up;
30
31         if (zero_diff_flow) {
32                 // These are shared between some of the diffusivities.
33                 vec2 flow_0_0 = get_flow_no_diff(0, 0);
34                 vec2 flow_1_1 = get_flow_no_diff(1, 1);
35
36                 // Find diffusivity (g) for this pixel, using central differences.
37                 {
38                         vec2 uv_x = get_flow_no_diff(1, 0) - get_flow_no_diff(-1,  0);
39                         vec2 uv_y = get_flow_no_diff(0, 1) - get_flow_no_diff( 0, -1);
40                         g = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
41                 }
42
43                 // Now find diffusivity for the pixel to the right.
44                 {
45                         vec2 uv_x = get_flow_no_diff(2, 0) - flow_0_0;
46                         vec2 uv_y = flow_1_1 - get_flow_no_diff( 1, -1);
47                         g_right = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
48                 }
49
50                 // And up.
51                 {
52                         vec2 uv_x = flow_1_1 - get_flow_no_diff(-1,  1);
53                         vec2 uv_y = get_flow_no_diff(0, 2) - flow_0_0;
54                         g_up = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
55                 }
56         } else {
57                 // These are shared between some of the diffusivities.
58                 vec2 flow_0_0 = get_flow(0, 0);
59                 vec2 flow_1_1 = get_flow(1, 1);
60
61                 // Find diffusivity (g) for this pixel, using central differences.
62                 {
63                         vec2 uv_x = get_flow(1, 0) - get_flow(-1,  0);
64                         vec2 uv_y = get_flow(0, 1) - get_flow( 0, -1);
65                         g = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
66                 }
67
68                 // Now find diffusivity for the pixel to the right.
69                 {
70                         vec2 uv_x = get_flow(2, 0) - flow_0_0;
71                         vec2 uv_y = flow_1_1 - get_flow( 1, -1);
72                         g_right = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
73                 }
74
75                 // And up.
76                 {
77                         vec2 uv_x = flow_1_1 - get_flow(-1,  1);
78                         vec2 uv_y = get_flow(0, 2) - flow_0_0;
79                         g_up = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
80                 }
81         }
82
83         smoothness_x = 0.5 * (g + g_right);
84         smoothness_y = 0.5 * (g + g_up);
85 }