struct CameraUniform {
  eye: vec4<f32>;
  vp: mat4x4<f32>;
};

struct MeshInstance {
  transform: mat4x4<f32>;
};

struct MeshData {
  instances: array<MeshInstance>;
};

struct PointLight {
  center: vec4<f32>;
  intensity: vec4<f32>;
};

struct PointLightData {
  num: i32;
  lights: array<PointLight>;
};

struct VertexInput {
  [[location(0)]] position: vec3<f32>;
  [[location(1)]] normal: vec3<f32>;
  [[location(2)]] tex_coords: vec2<f32>;
};

struct VertexOutput {
  [[builtin(position)]] clip_position: vec4<f32>;
  [[location(0)]] position: vec3<f32>;
  [[location(1)]] normal: vec3<f32>;
  [[location(2)]] tex_coords: vec2<f32>;
};

[[group(0), binding(0)]]
var<uniform> camera: CameraUniform;

[[group(0), binding(1)]]
var<storage,read> point_lights: PointLightData;

[[group(1), binding(0)]]
var<storage,read> meshes: MeshData;

[[group(2), binding(0)]] var m_sampler: sampler;
[[group(2), binding(1)]] var m_albedo: texture_2d<f32>;
[[group(2), binding(2)]] var m_metallic_roughness: texture_2d<f32>;

let PI: f32 = 3.141592;

fn D_GGX(NoH: f32, roughness: f32) -> f32 {
  let a = roughness * roughness;
  let a2 = a * a;
  let NoH2 = NoH * NoH;
  let f = NoH * (a2 - 1.0) + 1.0;
  return a2 / (PI * f * f);
}

fn g1(NoV: f32, roughness: f32, k: f32) -> f32 {
  let denom = NoV * (1.0 - k) + k;
  return NoV / denom;
}

fn G_SmithGGXCorrelated(NoV: f32, NoL: f32, roughness: f32) -> f32 {
  let r = roughness + 1.0;
  let k = (r * r) / 8.0;

  let g1l = g1(NoV, roughness, k);
  let g1v = g1(NoL, roughness, k);

  return g1l * g1v;
}

fn F_Schlick(u: f32, f0: vec3<f32>) -> vec3<f32> {
  let f = pow(1.0 - u, 5.0);
  return f + f0 * (1.0 - f);
}

fn BRDF(
  l: vec3<f32>,      // normalized light direction
  n: vec3<f32>,      // normalized surface normal
  v: vec3<f32>,      // normalized view direction
  albedo: vec3<f32>, // surface albedo
  metallic: f32,     // surface metallic
  roughness: f32,    // surface roughness
) -> vec3<f32> {
  let h = normalize(v + l);
  let NoL = max(dot(n, l), 0.0);
  let NoV = max(dot(n, v), 0.0);
  let NoH = max(dot(n, h), 0.0);
  let LoH = max(dot(l, h), 0.0);

  // calculate reflectance at surface incidence
  let f0 = mix(vec3<f32>(0.04), albedo, metallic);

  // specular BRDF
  let D = D_GGX(NoH, roughness);
  let G = G_SmithGGXCorrelated(NoV, NoL, roughness);
  let F = F_Schlick(LoH, f0);

  let numerator = (D * G) * F;
  let denominator = 4.0 * NoV * NoL;

  let Fr = numerator / max(denominator, 0.01);

  // diffuse BRDF
  let diffuse_fresnel =
    (vec3<f32>(1.0) - F_Schlick(NoL, f0)) *
    (vec3<f32>(1.0) - F_Schlick(NoV, f0));

  let lambertian = albedo / PI;
  let Fd = diffuse_fresnel * lambertian;

  // TODO multiple scattering
  return (Fr + Fd) * NoL;
}

fn sample_ibl_fake(
  n_color: vec3<f32>,
  s_color: vec3<f32>,
  gradient: f32,
  coords: vec3<f32>,
) -> vec3<f32> {
  let blend = coords.y / gradient + 0.5;
  return mix(s_color, n_color, clamp(blend, 0.0, 1.0));
}

fn F_Schlick_Roughness(cos_theta: f32, f0: vec3<f32>, roughness: f32) -> vec3<f32> {
  return f0 + (max(vec3<f32>(1.0 - roughness), f0) - f0) * pow(clamp(1.0 - cos_theta, 0.0, 1.0), 5.0);
}

let N_COLOR = vec3<f32>(0.7, 0.0, 1.0);
let S_COLOR = vec3<f32>(0.1, 0.1, 0.1);
let IBL_G = 1.0;

fn fake_ibl(
  n: vec3<f32>,      // normalized surface normal
  v: vec3<f32>,      // normalized view direction
  albedo: vec3<f32>, // surface albedo
  metallic: f32,     // surface metallic
  roughness: f32,    // surface roughness
) -> vec3<f32> {
  // calculate reflectance at surface incidence
  let f0 = mix(vec3<f32>(0.04), albedo, metallic);
  let kS = F_Schlick_Roughness(max(dot(n, v), 0.0), f0, roughness);
  let kD = 1.0 - kS;
  let diffuse = sample_ibl_fake(N_COLOR, S_COLOR, IBL_G, n);

  let R = normalize(reflect(-v, n));
  let specular = sample_ibl_fake(N_COLOR, S_COLOR, IBL_G, R);

  let received = kD * diffuse + kS * specular;

  return received * albedo;
}

[[stage(vertex)]]
fn vs_main(
  [[builtin(instance_index)]] mesh_idx: u32,
  vertex: VertexInput,
) -> VertexOutput {
  let transform = meshes.instances[mesh_idx].transform;
  let world_pos = transform * vec4<f32>(vertex.position, 1.0);
  let world_normal = transform * vec4<f32>(vertex.normal, 0.0);

  var out: VertexOutput;
  out.clip_position = camera.vp * world_pos;
  out.position = world_pos.xyz;
  out.normal = world_normal.xyz;
  out.tex_coords = vertex.tex_coords;
  return out;
}

[[stage(fragment)]]
fn fs_main(
  frag: VertexOutput,
) -> [[location(0)]] vec4<f32> {
  let albedo = textureSample(m_albedo, m_sampler, frag.tex_coords).rgb;
  let normal = normalize(frag.normal);
  let view = normalize(camera.eye.xyz - frag.position);

  let metallic_roughness = textureSample(m_metallic_roughness, m_sampler, frag.tex_coords).bg;
  let metallic = metallic_roughness.x;
  let roughness = metallic_roughness.y;

  var lum = vec3<f32>(0.0);
  for(var i = 0; i < 4; i = i + 1) {
    let light = point_lights.lights[i];
    let light_position = light.center.xyz - frag.position;
    let light_intensity = light.intensity.rgb;
    let light_direction = normalize(light_position);

    let radiance = light_intensity / dot(light_position, light_position);

    let reflected = BRDF(
      light_direction, normal, view,
      albedo, metallic, roughness
    );

    lum = lum + (radiance * reflected);
  }

  lum = lum + fake_ibl(normal, view, albedo, metallic, roughness);

  let tone_mapped = lum / (lum + 1.0);
  return vec4<f32>(tone_mapped, 1.0);
}
