]> git.sesse.net Git - nageru/blob - derivatives.frag
Use textureOffset() wherever possible to save some tedious arithmetic.
[nageru] / derivatives.frag
1 #version 450 core
2
3 in vec2 tc;
4 out vec2 derivatives;
5
6 uniform sampler2D tex;
7
8 void main()
9 {
10         float x_m2 = textureOffset(tex, tc, ivec2(-2,  0)).x;
11         float x_m1 = textureOffset(tex, tc, ivec2(-1,  0)).x;
12         float x_p1 = textureOffset(tex, tc, ivec2( 1,  0)).x;
13         float x_p2 = textureOffset(tex, tc, ivec2( 2,  0)).x;
14
15         float y_m2 = textureOffset(tex, tc, ivec2( 0, -2)).x;
16         float y_m1 = textureOffset(tex, tc, ivec2( 0, -1)).x;
17         float y_p1 = textureOffset(tex, tc, ivec2( 0,  1)).x;
18         float y_p2 = textureOffset(tex, tc, ivec2( 0,  2)).x;
19
20         derivatives.x = (x_p1 - x_m1) * (2.0/3.0) + (x_m2 - x_p2) * (1.0/12.0);
21         derivatives.y = (y_p1 - y_m1) * (2.0/3.0) + (y_m2 - y_p2) * (1.0/12.0);
22 }