Debian Codegraph @

shaders/src/ambient_occlusion.fs

raw
1 //------------------------------------------------------------------------------
2 // Ambient occlusion configuration
3 //------------------------------------------------------------------------------
4
5 // Diffuse BRDFs
6 #define SPECULAR_AO_OFF 0
7 #define SPECULAR_AO_SIMPLE 1
8 #define SPECULAR_AO_BENT_NORMALS 2
9
10 //------------------------------------------------------------------------------
11 // Ambient occlusion helpers
12 //------------------------------------------------------------------------------
13
14 float unpack(vec2 depth) {
15 // this is equivalent to (x8 * 256 + y8) / 65535, which gives a value between 0 and 1
16 return (depth.x * (256.0 / 257.0) + depth.y * (1.0 / 257.0));
17 }
18
19 float evaluateSSAO() {
20 #if defined(BLEND_MODE_OPAQUE) || defined(BLEND_MODE_MASKED)
21 highp vec2 uv = uvToRenderTargetUV(getNormalizedViewportCoord().xy);
22
23 // Upscale the SSAO buffer in real-time, in high quality mode we use a custom bilinear
24 // filter. This adds about 2.0ms @ 250MHz on Pixel 4.
25
26 if (frameUniforms.aoSamplingQualityAndEdgeDistance > 0.0) {
27 highp vec2 size = vec2(textureSize(light_ssao, 0));
28
29 // Read four AO samples and their depths values
30 #if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER)
31 vec4 ao = textureGather(light_ssao, uv, 0);
32 vec4 dg = textureGather(light_ssao, uv, 1);
33 vec4 db = textureGather(light_ssao, uv, 2);
34 #else
35 vec3 s01 = textureLodOffset(light_ssao, uv, 0.0, ivec2(0, 1)).rgb;
36 vec3 s11 = textureLodOffset(light_ssao, uv, 0.0, ivec2(1, 1)).rgb;
37 vec3 s10 = textureLodOffset(light_ssao, uv, 0.0, ivec2(1, 0)).rgb;
38 vec3 s00 = textureLodOffset(light_ssao, uv, 0.0, ivec2(0, 0)).rgb;
39 vec4 ao = vec4(s01.r, s11.r, s10.r, s00.r);
40 vec4 dg = vec4(s01.g, s11.g, s10.g, s00.g);
41 vec4 db = vec4(s01.b, s11.b, s10.b, s00.b);
42 #endif
43 // bilinear weights
44 vec2 f = fract(uv * size - 0.5);
45 vec4 b;
46 b.x = (1.0 - f.x) * f.y;
47 b.y = f.x * f.y;
48 b.z = f.x * (1.0 - f.y);
49 b.w = (1.0 - f.x) * (1.0 - f.y);
50
51 // bilateral weights
52 vec4 depths;
53 depths.x = unpack(vec2(dg.x, db.x));
54 depths.y = unpack(vec2(dg.y, db.y));
55 depths.z = unpack(vec2(dg.z, db.z));
56 depths.w = unpack(vec2(dg.w, db.w));
57 depths *= -frameUniforms.cameraFar;
58 highp mat4 m = getViewFromWorldMatrix();
59 highp float d = dot(vec3(m[0].z, m[1].z, m[2].z), shading_position) + m[3].z;
60 highp vec4 w = (vec4(d) - depths) * frameUniforms.aoSamplingQualityAndEdgeDistance;
61 w = max(vec4(MEDIUMP_FLT_MIN), 1.0 - w * w) * b;
62 return dot(ao, w) * (1.0 / (w.x + w.y + w.z + w.w));
63 } else {
64 return textureLod(light_ssao, uv, 0.0).r;
65 }
66 #else
67 // SSAO is not applied when blending is enabled
68 return 1.0;
69 #endif
70 }
71
72 float SpecularAO_Lagarde(float NoV, float visibility, float roughness) {
73 // Lagarde and de Rousiers 2014, "Moving Frostbite to PBR"
74 return saturate(pow(NoV + visibility, exp2(-16.0 * roughness - 1.0)) - 1.0 + visibility);
75 }
76
77 #if defined(MATERIAL_HAS_BENT_NORMAL)
78 float sphericalCapsIntersection(float cosCap1, float cosCap2, float cosDistance) {
79 // Oat and Sander 2007, "Ambient Aperture Lighting"
80 // Approximation mentioned by Jimenez et al. 2016
81 float r1 = acosFastPositive(cosCap1);
82 float r2 = acosFastPositive(cosCap2);
83 float d = acosFast(cosDistance);
84
85 // We work with cosine angles, replace the original paper's use of
86 // cos(min(r1, r2)) with max(cosCap1, cosCap2)
87 // We also remove a multiplication by 2 * PI to simplify the computation
88 // since we divide by 2 * PI in computeBentSpecularAO()
89
90 if (min(r1, r2) <= max(r1, r2) - d) {
91 return 1.0 - max(cosCap1, cosCap2);
92 } else if (r1 + r2 <= d) {
93 return 0.0;
94 }
95
96 float delta = abs(r1 - r2);
97 float x = 1.0 - saturate((d - delta) / max(r1 + r2 - delta, 1e-4));
98 // simplified smoothstep()
99 float area = sq(x) * (-2.0 * x + 3.0);
100 return area * (1.0 - max(cosCap1, cosCap2));
101 }
102 #endif
103
104 // This function could (should?) be implemented as a 3D LUT instead, but we need to save samplers
105 float SpecularAO_Cones(float NoV, float visibility, float roughness) {
106 #if defined(MATERIAL_HAS_BENT_NORMAL)
107 // Jimenez et al. 2016, "Practical Realtime Strategies for Accurate Indirect Occlusion"
108
109 // aperture from ambient occlusion
110 float cosAv = sqrt(1.0 - visibility);
111 // aperture from roughness, log(10) / log(2) = 3.321928
112 float cosAs = exp2(-3.321928 * sq(roughness));
113 // angle betwen bent normal and reflection direction
114 float cosB = dot(shading_bentNormal, shading_reflected);
115
116 // Remove the 2 * PI term from the denominator, it cancels out the same term from
117 // sphericalCapsIntersection()
118 float ao = sphericalCapsIntersection(cosAv, cosAs, cosB) / (1.0 - cosAs);
119 // Smoothly kill specular AO when entering the perceptual roughness range [0.1..0.3]
120 // Without this, specular AO can remove all reflections, which looks bad on metals
121 return mix(1.0, ao, smoothstep(0.01, 0.09, roughness));
122 #else
123 return SpecularAO_Lagarde(NoV, visibility, roughness);
124 #endif
125 }
126
127 /**
128 * Computes a specular occlusion term from the ambient occlusion term.
129 */
130 float computeSpecularAO(float NoV, float visibility, float roughness) {
131 #if SPECULAR_AMBIENT_OCCLUSION == SPECULAR_AO_SIMPLE
132 return SpecularAO_Lagarde(NoV, visibility, roughness);
133 #elif SPECULAR_AMBIENT_OCCLUSION == SPECULAR_AO_BENT_NORMALS
134 return SpecularAO_Cones(NoV, visibility, roughness);
135 #else
136 return 1.0;
137 #endif
138 }
139
140 #if MULTI_BOUNCE_AMBIENT_OCCLUSION == 1
141 /**
142 * Returns a color ambient occlusion based on a pre-computed visibility term.
143 * The albedo term is meant to be the diffuse color or f0 for the diffuse and
144 * specular terms respectively.
145 */
146 vec3 gtaoMultiBounce(float visibility, const vec3 albedo) {
147 // Jimenez et al. 2016, "Practical Realtime Strategies for Accurate Indirect Occlusion"
148 vec3 a = 2.0404 * albedo - 0.3324;
149 vec3 b = -4.7951 * albedo + 0.6417;
150 vec3 c = 2.7552 * albedo + 0.6903;
151
152 return max(vec3(visibility), ((visibility * a + b) * visibility + c) * visibility);
153 }
154 #endif
155
156 void multiBounceAO(float visibility, const vec3 albedo, inout vec3 color) {
157 #if MULTI_BOUNCE_AMBIENT_OCCLUSION == 1
158 color *= gtaoMultiBounce(visibility, albedo);
159 #endif
160 }
161
162 void multiBounceSpecularAO(float visibility, const vec3 albedo, inout vec3 color) {
163 #if MULTI_BOUNCE_AMBIENT_OCCLUSION == 1 && SPECULAR_AMBIENT_OCCLUSION != SPECULAR_AO_OFF
164 color *= gtaoMultiBounce(visibility, albedo);
165 #endif
166 }
167
168 float singleBounceAO(float visibility) {
169 #if MULTI_BOUNCE_AMBIENT_OCCLUSION == 1
170 return 1.0;
171 #else
172 return visibility;
173 #endif
174 }