Debian Codegraph @

code/renderergl2/glsl/ssao_fp.glsl

raw
1 uniform sampler2D u_ScreenDepthMap;
2
3 uniform vec4 u_ViewInfo; // zfar / znear, zfar, 1/width, 1/height
4
5 varying vec2 var_ScreenTex;
6
7 #if 0
8 vec2 poissonDisc[9] = vec2[9](
9 vec2(-0.7055767, 0.196515), vec2(0.3524343, -0.7791386),
10 vec2(0.2391056, 0.9189604), vec2(-0.07580382, -0.09224417),
11 vec2(0.5784913, -0.002528916), vec2(0.192888, 0.4064181),
12 vec2(-0.6335801, -0.5247476), vec2(-0.5579782, 0.7491854),
13 vec2(0.7320465, 0.6317794)
14 );
15 #endif
16
17 #define NUM_SAMPLES 3
18
19 // Input: It uses texture coords as the random number seed.
20 // Output: Random number: [0,1), that is between 0.0 and 0.999999... inclusive.
21 // Author: Michael Pohoreski
22 // Copyright: Copyleft 2012 :-)
23 // Source: http://stackoverflow.com/questions/5149544/can-i-generate-a-random-number-inside-a-pixel-shader
24
25 float random( const vec2 p )
26 {
27 // We need irrationals for pseudo randomness.
28 // Most (all?) known transcendental numbers will (generally) work.
29 const vec2 r = vec2(
30 23.1406926327792690, // e^pi (Gelfond's constant)
31 2.6651441426902251); // 2^sqrt(2) (Gelfond-Schneider constant)
32 //return fract( cos( mod( 123456789., 1e-7 + 256. * dot(p,r) ) ) );
33 return mod( 123456789., 1e-7 + 256. * dot(p,r) );
34 }
35
36 mat2 randomRotation( const vec2 p )
37 {
38 float r = random(p);
39 float sinr = sin(r);
40 float cosr = cos(r);
41 return mat2(cosr, sinr, -sinr, cosr);
42 }
43
44 float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear)
45 {
46 float sampleZDivW = texture2D(depthMap, tex).r;
47 return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW);
48 }
49
50 float ambientOcclusion(sampler2D depthMap, const vec2 tex, const float zFarDivZNear, const float zFar, const vec2 scale)
51 {
52 vec2 poissonDisc[9];
53
54 poissonDisc[0] = vec2(-0.7055767, 0.196515);
55 poissonDisc[1] = vec2(0.3524343, -0.7791386);
56 poissonDisc[2] = vec2(0.2391056, 0.9189604);
57 poissonDisc[3] = vec2(-0.07580382, -0.09224417);
58 poissonDisc[4] = vec2(0.5784913, -0.002528916);
59 poissonDisc[5] = vec2(0.192888, 0.4064181);
60 poissonDisc[6] = vec2(-0.6335801, -0.5247476);
61 poissonDisc[7] = vec2(-0.5579782, 0.7491854);
62 poissonDisc[8] = vec2(0.7320465, 0.6317794);
63
64 float result = 0.0;
65
66 float sampleZ = getLinearDepth(depthMap, tex, zFarDivZNear);
67 float scaleZ = zFarDivZNear * sampleZ;
68
69 vec2 slope = vec2(dFdx(sampleZ), dFdy(sampleZ)) / vec2(dFdx(tex.x), dFdy(tex.y));
70
71 if (length(slope) * zFar > 5000.0)
72 return 1.0;
73
74 vec2 offsetScale = vec2(scale * 1024.0 / scaleZ);
75
76 mat2 rmat = randomRotation(tex);
77
78 float invZFar = 1.0 / zFar;
79 float zLimit = 20.0 * invZFar;
80 for (int i = 0; i < NUM_SAMPLES; i++)
81 {
82 vec2 offset = rmat * poissonDisc[i] * offsetScale;
83 float sampleDiff = getLinearDepth(depthMap, tex + offset, zFarDivZNear) - sampleZ;
84
85 bool s1 = abs(sampleDiff) > zLimit;
86 bool s2 = sampleDiff + invZFar > dot(slope, offset);
87 result += float(s1 || s2);
88 }
89
90 result *= 1.0 / float(NUM_SAMPLES);
91
92 return result;
93 }
94
95 void main()
96 {
97 float result = ambientOcclusion(u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.wz);
98
99 gl_FragColor = vec4(vec3(result), 1.0);
100 }