2018-08-27 07:31:48 +02:00
|
|
|
/* clang-format off */
|
2016-12-04 16:45:30 +01:00
|
|
|
[vertex]
|
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
layout(location = 0) in highp vec4 vertex_attrib;
|
2018-08-27 07:31:48 +02:00
|
|
|
/* clang-format on */
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
|
|
|
gl_Position = vertex_attrib;
|
2018-08-24 13:42:18 +02:00
|
|
|
gl_Position.z = 1.0;
|
2016-12-04 16:45:30 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 07:31:48 +02:00
|
|
|
/* clang-format off */
|
2016-12-04 16:45:30 +01:00
|
|
|
[fragment]
|
|
|
|
|
|
|
|
uniform sampler2D source_ssao; //texunit:0
|
2018-08-27 07:31:48 +02:00
|
|
|
/* clang-format on */
|
2016-12-04 16:45:30 +01:00
|
|
|
uniform sampler2D source_depth; //texunit:1
|
2017-10-22 18:52:31 +02:00
|
|
|
uniform sampler2D source_normal; //texunit:3
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
layout(location = 0) out float visibility;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Tunable Parameters:
|
|
|
|
|
|
|
|
/** Increase to make depth edges crisper. Decrease to reduce flicker. */
|
2017-10-22 18:52:31 +02:00
|
|
|
uniform float edge_sharpness;
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
/** Step in 2-pixel intervals since we already blurred against neighbors in the
|
2018-08-24 13:42:18 +02:00
|
|
|
first AO pass. This constant can be increased while R decreases to improve
|
|
|
|
performance at the expense of some dithering artifacts.
|
2016-12-04 16:45:30 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
Morgan found that a scale of 3 left a 1-pixel checkerboard grid that was
|
|
|
|
unobjectionable after shading was applied but eliminated most temporal incoherence
|
|
|
|
from using small numbers of sample taps.
|
|
|
|
*/
|
2017-10-22 18:52:31 +02:00
|
|
|
|
|
|
|
uniform int filter_scale;
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
/** Filter radius in pixels. This will be multiplied by SCALE. */
|
2018-08-24 13:42:18 +02:00
|
|
|
#define R (4)
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Gaussian coefficients
|
|
|
|
const float gaussian[R + 1] =
|
2018-08-27 07:31:48 +02:00
|
|
|
//float[](0.356642, 0.239400, 0.072410, 0.009869);
|
|
|
|
//float[](0.398943, 0.241971, 0.053991, 0.004432, 0.000134); // stddev = 1.0
|
|
|
|
float[](0.153170, 0.144893, 0.122649, 0.092902, 0.062970); // stddev = 2.0
|
|
|
|
//float[](0.111220, 0.107798, 0.098151, 0.083953, 0.067458, 0.050920, 0.036108); // stddev = 3.0
|
2016-12-04 16:45:30 +01:00
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
/** (1, 0) or (0, 1) */
|
|
|
|
uniform ivec2 axis;
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
uniform float camera_z_far;
|
|
|
|
uniform float camera_z_near;
|
|
|
|
|
2017-08-29 16:46:02 +02:00
|
|
|
uniform ivec2 screen_size;
|
|
|
|
|
2016-12-04 16:45:30 +01:00
|
|
|
void main() {
|
|
|
|
|
|
|
|
ivec2 ssC = ivec2(gl_FragCoord.xy);
|
|
|
|
|
|
|
|
float depth = texelFetch(source_depth, ssC, 0).r;
|
2018-08-24 13:42:18 +02:00
|
|
|
//vec3 normal = texelFetch(source_normal, ssC, 0).rgb * 2.0 - 1.0;
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
depth = depth * 2.0 - 1.0;
|
|
|
|
depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near));
|
|
|
|
|
|
|
|
float depth_divide = 1.0 / camera_z_far;
|
|
|
|
|
2018-08-24 13:42:18 +02:00
|
|
|
//depth *= depth_divide;
|
2016-12-04 16:45:30 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
2018-08-24 13:42:18 +02:00
|
|
|
if (depth > camera_z_far * 0.999) {
|
|
|
|
discard; //skybox
|
2017-01-14 12:26:56 +01:00
|
|
|
}
|
|
|
|
*/
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
float sum = texelFetch(source_ssao, ssC, 0).r;
|
|
|
|
|
|
|
|
// Base weight for depth falloff. Increase this for more blurriness,
|
|
|
|
// decrease it for better edge discrimination
|
|
|
|
float BASE = gaussian[0];
|
|
|
|
float totalWeight = BASE;
|
|
|
|
sum *= totalWeight;
|
|
|
|
|
2017-08-29 16:46:02 +02:00
|
|
|
ivec2 clamp_limit = screen_size - ivec2(1);
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
for (int r = -R; r <= R; ++r) {
|
|
|
|
// We already handled the zero case above. This loop should be unrolled and the static branch optimized out,
|
|
|
|
// so the IF statement has no runtime cost
|
|
|
|
if (r != 0) {
|
|
|
|
|
2017-10-22 18:52:31 +02:00
|
|
|
ivec2 ppos = ssC + axis * (r * filter_scale);
|
2018-08-24 13:42:18 +02:00
|
|
|
float value = texelFetch(source_ssao, clamp(ppos, ivec2(0), clamp_limit), 0).r;
|
|
|
|
ivec2 rpos = clamp(ppos, ivec2(0), clamp_limit);
|
2017-10-22 18:52:31 +02:00
|
|
|
float temp_depth = texelFetch(source_depth, rpos, 0).r;
|
|
|
|
//vec3 temp_normal = texelFetch(source_normal, rpos, 0).rgb * 2.0 - 1.0;
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
temp_depth = temp_depth * 2.0 - 1.0;
|
|
|
|
temp_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - temp_depth * (camera_z_far - camera_z_near));
|
2018-08-24 13:42:18 +02:00
|
|
|
//temp_depth *= depth_divide;
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
// spatial domain: offset gaussian tap
|
|
|
|
float weight = 0.3 + gaussian[abs(r)];
|
2018-08-24 13:42:18 +02:00
|
|
|
//weight *= max(0.0, dot(temp_normal, normal));
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
// range domain (the "bilateral" weight). As depth difference increases, decrease weight.
|
2018-08-24 13:42:18 +02:00
|
|
|
weight *= max(0.0, 1.0 - edge_sharpness * abs(temp_depth - depth));
|
2016-12-04 16:45:30 +01:00
|
|
|
|
|
|
|
sum += value * weight;
|
|
|
|
totalWeight += weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const float epsilon = 0.0001;
|
|
|
|
visibility = sum / (totalWeight + epsilon);
|
|
|
|
}
|