#pragma language glsl4

//uniform sampler2D g_color_sampler;
uniform sampler2D g_position_sampler;
uniform sampler2D g_normal_sampler;
uniform sampler2D noise_sampler;

uniform mat4 projection;
uniform vec3 sample_kernel[64];

const vec2 noise_scale = vec2(1024.0 / 4.0, 1024 / 4.0);

uniform float bias1;
uniform float radius1;
uniform float occlusion_exponent1;
uniform float occlusion_offset1;

uniform float bias;
uniform float radius;
uniform float occlusion_exponent;
uniform float occlusion_offset;

const int samples = 64;

varying vec4 PixelTexture;

out vec2 out_occlusion;

layout (std430) readonly buffer SSAOKernelLayout
{
  vec4 SSAOKernel[];
};

float kernel_depth(vec3 position, vec3 sample_position)
{
  vec4 offset = vec4(sample_position, 1.0);
  offset = projection * offset;
  offset.xyz = offset.xyz / offset.w;

  vec2 sample_depth_coord = offset.xy * vec2(0.5, -0.5) + 0.5;
  float sample_depth = texture(g_position_sampler, sample_depth_coord).z;
  return sample_depth;
}

void pixelmain()
{
  //vec3 color = texture(g_color_sampler, PixelTexture.xy).xyz;
  vec3 position = texture(g_position_sampler, PixelTexture.xy).xyz;
  vec3 normal = normalize(texture(g_normal_sampler, PixelTexture.xy).xyz);
  vec3 noise = normalize(vec3(texture(noise_sampler, PixelTexture.xy * noise_scale).xy, 0));

  vec3 tangent = normalize(noise - normal * dot(noise, normal));
  vec3 bitangent = cross(normal, tangent);
  mat3 TBN = mat3(tangent, bitangent, normal);

  float occlusion1 = 0.0;
  float occlusion = 0.0;
  for (int i = 0; i < samples; i++) {
    vec3 sample_position = (TBN * SSAOKernel[i].xyz) * radius + position;
    vec3 sample_position1 = (SSAOKernel[i].xyz) * radius1 + position;

    float sample_depth = kernel_depth(position, sample_position);
    float sample_depth1 = kernel_depth(position, sample_position1);

    float range_check = smoothstep(0.0, 1.0, radius / abs(position.z - sample_depth));
    occlusion += (sample_depth >= sample_position.z + bias ? 1.0 : 0.0) * range_check;

    float range_check1 = smoothstep(0.0, 1.0, radius1 / abs(position.z - sample_depth1));
    occlusion1 += (sample_depth1 >= sample_position1.z + bias1 ? 1.0 : 0.0) * range_check1;
  }
  occlusion = 1.0 - (occlusion / samples);
  occlusion = pow(occlusion, occlusion_exponent) + occlusion_offset;

  occlusion1 = 1.0 - (occlusion1 / samples);
  occlusion1 = pow(occlusion1, occlusion_exponent1) + occlusion_offset1;

  //out_color = vec4(color * occlusion * occlusion1, 1.0);
  out_occlusion = vec2(occlusion, occlusion1);
}
