|
|
@@ -6,21 +6,6 @@
|
|
|
|
|
|
#pragma anki include "shaders/Pack.glsl"
|
|
|
|
|
|
-/// @name Uniforms
|
|
|
-/// @{
|
|
|
-uniform vec2 planes; ///< for the calculation of frag pos in view space
|
|
|
-uniform vec2 limitsOfNearPlane; ///< for the calculation of frag pos in view space
|
|
|
-uniform vec2 limitsOfNearPlane2; ///< This is an optimization see r403 for the clean one
|
|
|
-uniform float zNear; ///< for the calculation of frag pos in view space
|
|
|
-uniform sampler2D msDepthFai; ///< for the calculation of frag pos in view space
|
|
|
-
|
|
|
-uniform sampler2D noiseMap; /// Used in getRandom
|
|
|
-uniform float noiseMapSize = 0.0; /// Used in getRandom
|
|
|
-uniform vec2 screenSize = vec2(0.0); /// Used in getRandom
|
|
|
-
|
|
|
-uniform sampler2D msNormalFai; /// Used in getNormal
|
|
|
-/// @}
|
|
|
-
|
|
|
/// @name Varyings
|
|
|
/// @{
|
|
|
in vec2 vTexCoords;
|
|
|
@@ -31,31 +16,51 @@ in vec2 vTexCoords;
|
|
|
layout(location = 0) out float fColor;
|
|
|
/// @}
|
|
|
|
|
|
-/// @name Consts
|
|
|
+
|
|
|
+/// @name Uniforms
|
|
|
/// @{
|
|
|
-uniform float SAMPLE_RAD = 0.08; /// Used in main
|
|
|
-uniform float SCALE = 1.0; /// Used in doAmbientOcclusion
|
|
|
-uniform float INTENSITY = 3.0; /// Used in doAmbientOcclusion
|
|
|
-uniform float BIAS = 0.00; /// Used in doAmbientOcclusion
|
|
|
+layout(std140, row_major, binding = 0) uniform commonBlock
|
|
|
+{
|
|
|
+ /// Packs:
|
|
|
+ /// - x: zNear. For the calculation of frag pos in view space
|
|
|
+ /// - zw: Planes. For the calculation of frag pos in view space
|
|
|
+ uniform vec4 nearPlanes;
|
|
|
+
|
|
|
+ /// For the calculation of frag pos in view space. The xy is the
|
|
|
+ /// limitsOfNearPlane and the zw is an optimization see PpsSsao.glsl and
|
|
|
+ /// r403 for the clean one
|
|
|
+ uniform vec4 limitsOfNearPlane_;
|
|
|
+};
|
|
|
+
|
|
|
+#define planes nearPlanes.zw
|
|
|
+#define zNear nearPlanes.x
|
|
|
+#define limitsOfNearPlane limitsOfNearPlane_.xy
|
|
|
+#define limitsOfNearPlane2 limitsOfNearPlane_.zw
|
|
|
+
|
|
|
+uniform sampler2D msDepthFai;
|
|
|
+uniform usampler2D msGFai;
|
|
|
+uniform sampler2D noiseMap;
|
|
|
/// @}
|
|
|
|
|
|
+#define SAMPLE_RAD 0.08
|
|
|
+#define SCALE 1.0
|
|
|
+#define INTENSITY 3.0
|
|
|
+#define BIAS 0.00
|
|
|
|
|
|
-/// globals: msNormalFai
|
|
|
vec3 getNormal(in vec2 uv)
|
|
|
{
|
|
|
- return unpackNormal(texture2D(msNormalFai, uv).rg);
|
|
|
+ uvec2 msAll = texture(msGFai, uv).rg;
|
|
|
+ vec3 normal = unpackNormal(unpackHalf2x16(msAll[1]));
|
|
|
+ return normal;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-/// globals: noiseMap, screenSize, noiseMapSize
|
|
|
vec2 getRandom(in vec2 uv)
|
|
|
{
|
|
|
- return normalize(texture2D(noiseMap, screenSize * uv / noiseMapSize).xy * 2.0 - 1.0);
|
|
|
+ vec2 noise = texture2D(noiseMap,
|
|
|
+ vec2(WIDTH, HEIGHT) * uv / NOISE_MAP_SIZE / 2.0).xy;
|
|
|
+ return normalize(noise * 2.0 - 1.0);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-/// Get frag position in view space
|
|
|
-/// globals: msDepthFai, planes, zNear, limitsOfNearPlane
|
|
|
vec3 getPosition(in vec2 uv)
|
|
|
{
|
|
|
float depth = texture2D(msDepthFai, uv).r;
|
|
|
@@ -63,18 +68,15 @@ vec3 getPosition(in vec2 uv)
|
|
|
vec3 fragPosVspace;
|
|
|
fragPosVspace.z = -planes.y / (planes.x + depth);
|
|
|
|
|
|
- // Old: fragPosVspace.xy = (uv * 2.0 - 1.0) * limitsOfNearPlane;
|
|
|
fragPosVspace.xy = (uv * limitsOfNearPlane2) - limitsOfNearPlane;
|
|
|
|
|
|
- float sc = -fragPosVspace.z / zNear;
|
|
|
+ const float sc = -fragPosVspace.z / zNear;
|
|
|
fragPosVspace.xy *= sc;
|
|
|
|
|
|
return fragPosVspace;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-/// Calculate the ambient occlusion factor
|
|
|
-float doAmbientOcclusion(in vec2 uv, in vec3 original, in vec3 cnorm)
|
|
|
+float calcAmbientOcclusionFactor(in vec2 uv, in vec3 original, in vec3 cnorm)
|
|
|
{
|
|
|
vec3 newp = getPosition(uv);
|
|
|
vec3 diff = newp - original;
|
|
|
@@ -85,20 +87,19 @@ float doAmbientOcclusion(in vec2 uv, in vec3 original, in vec3 cnorm)
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
+#define KERNEL_SIZE 16
|
|
|
+const vec2 KERNEL[KERNEL_SIZE] = vec2[](
|
|
|
+ vec2(0.53812504, 0.18565957), vec2(0.13790712, 0.24864247),
|
|
|
+ vec2(0.33715037, 0.56794053), vec2(-0.6999805, -0.04511441),
|
|
|
+ vec2(0.06896307, -0.15983082), vec2(0.056099437, 0.006954967),
|
|
|
+ vec2(-0.014653638, 0.14027752), vec2(0.010019933, -0.1924225),
|
|
|
+ vec2(-0.35775623, -0.5301969), vec2(-0.3169221, 0.106360726),
|
|
|
+ vec2(0.010350345, -0.58698344), vec2(-0.08972908, -0.49408212),
|
|
|
+ vec2(0.7119986, -0.0154690035), vec2(-0.053382345, 0.059675813),
|
|
|
+ vec2(0.035267662, -0.063188605), vec2(-0.47761092, 0.2847911));
|
|
|
|
|
|
void main(void)
|
|
|
{
|
|
|
- const int KERNEL_SIZE = 16;
|
|
|
-
|
|
|
- const vec2 KERNEL[KERNEL_SIZE] = vec2[](vec2(0.53812504, 0.18565957), vec2(0.13790712, 0.24864247),
|
|
|
- vec2(0.33715037, 0.56794053), vec2(-0.6999805, -0.04511441),
|
|
|
- vec2(0.06896307, -0.15983082), vec2(0.056099437, 0.006954967),
|
|
|
- vec2(-0.014653638, 0.14027752), vec2(0.010019933, -0.1924225),
|
|
|
- vec2(-0.35775623, -0.5301969), vec2(-0.3169221, 0.106360726),
|
|
|
- vec2(0.010350345, -0.58698344), vec2(-0.08972908, -0.49408212),
|
|
|
- vec2(0.7119986, -0.0154690035), vec2(-0.053382345, 0.059675813),
|
|
|
- vec2(0.035267662, -0.063188605), vec2(-0.47761092, 0.2847911));
|
|
|
-
|
|
|
vec3 p = getPosition(vTexCoords);
|
|
|
vec3 n = getNormal(vTexCoords);
|
|
|
vec2 rand = getRandom(vTexCoords);
|
|
|
@@ -109,11 +110,9 @@ void main(void)
|
|
|
for(int j = 0; j < KERNEL_SIZE; ++j)
|
|
|
{
|
|
|
vec2 coord = reflect(KERNEL[j], rand) * SAMPLE_RAD;
|
|
|
- fColor += doAmbientOcclusion(vTexCoords + coord, p, n);
|
|
|
+ fColor += calcAmbientOcclusionFactor(vTexCoords + coord, p, n);
|
|
|
}
|
|
|
|
|
|
fColor = 1.0 - fColor / KERNEL_SIZE;
|
|
|
-
|
|
|
- //fColor = fColor - fColor + rand.x;
|
|
|
}
|
|
|
|