aboutsummaryrefslogtreecommitdiff
path: root/shaders/brdf_integration_map.frag
blob: bb2cebd779daf59775420efd616abdad95da16f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
precision highp float;

#define PI 3.1415926535897932384626433832795
#define NUM_SAMPLES 1024

in vec2 Texcoord;

layout (location = 0) out vec2 Color;

float radical_inverse_VdC(uint bits) {
    bits = (bits << 16u) | (bits >> 16u);
    bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
    bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
    bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
    bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
    return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}

vec2 hammersley(uint i, uint N) {
    return vec2(float(i)/float(N), radical_inverse_VdC(i));
}

vec3 importance_sample_GGX(vec2 sample_box, vec3 N, float roughness) {
  float r2 = roughness * roughness;

  // Spherical coordinates.
  float phi = 2.0 * PI * sample_box.x;
  float cos_theta = sqrt((1.0 - sample_box.y) / (1.0 + (r2*r2 - 1.0) * sample_box.y));
  float sin_theta = sqrt(1.0 - cos_theta * cos_theta);

  // Map spherical coordinates to Cartesian coordinates in tangent space.
  vec3 H = vec3(cos(phi) * sin_theta, sin(phi) * sin_theta, cos_theta);

  // Map from tangent space to world space.
  //
  // Tangent space:
  //
  //        N
  //        |
  //        |
  //        |
  //        |_ _ _ _ _ B
  //       /
  //      /
  //     T
  vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
  vec3 T = normalize(cross(up,N));
  vec3 B = cross(N,T);
  vec3 H_ws = H.x*T + H.y*B + H.z*N;
  return H_ws;
}

float geometry_schlick_GGX(float k, float NdotV) {
  return NdotV / (NdotV * (1.0 - k) + k);
}

float geometry_smith(float roughness, float NdotL, float NdotV) {
  float k = roughness * roughness / 2.0; // IBL
  return geometry_schlick_GGX(k, NdotV) * geometry_schlick_GGX(k, NdotL);
}

vec2 integrate_brdf(float NdotV, float roughness)
{
  vec3 V = vec3(sqrt(1.0 - NdotV * NdotV), 0.0, NdotV);
  vec3 N = vec3(0.0, 0.0, 1.0);

  float scale = 0.0;
  float bias = 0.0;
  for (int i = 0; i < NUM_SAMPLES; ++i) {
    vec2 sample_box = hammersley(i, NUM_SAMPLES);
    vec3 H = importance_sample_GGX(sample_box, N, roughness);
    vec3 L = reflect(-V,H);
    float NdotL = max(0.0, L.z);

    if (NdotL > 0.0) {
      float NdotH = max(0.0, H.z);
      float VdotH = max(0.0, dot(V,H));
      float G = geometry_smith(roughness, NdotL, NdotV);
      float G_vis = (G * VdotH) / (NdotH * NdotV);
      float Fc = pow(1.0 - VdotH, 5.0);
      scale += (1.0 - Fc) * G_vis;
      bias  += Fc * G_vis;
    }
  }
  scale /= float(NUM_SAMPLES);
  bias  /= float(NUM_SAMPLES);
  return vec2(scale, bias);
}

void main()
{
  Color = integrate_brdf(Texcoord.x, Texcoord.y);
}