Debian Codegraph @

libs/iblprefilter/src/materials/generateKernel.mat

raw
1 material {
2 name : generateKernel,
3 parameters : [
4 {
5 type : uint2,
6 name : size,
7 precision: high
8 },
9 {
10 type : float,
11 name : sampleCount,
12 },
13 {
14 type : float,
15 name : oneOverLevelsMinusOne,
16 }
17 ],
18 outputs : [
19 {
20 name : weight,
21 target : color,
22 type : float4
23 }
24 ],
25 variables : [
26 vertex
27 ],
28 domain : postprocess,
29 depthWrite : false,
30 depthCulling : false
31 }
32
33 vertex {
34 void postProcessVertex(inout PostProcessVertexInputs postProcess) {
35 postProcess.vertex.xy = postProcess.normalizedUV * vec2(materialParams.size);
36 }
37 }
38
39 fragment {
40
41 void dummy() {}
42
43 // we default to highp in this shader
44 precision highp float;
45
46 float log4(const float x) {
47 return log2(x) * 0.5;
48 }
49
50 vec2 hammersley(const uint index, const float sampleCount) {
51 float invNumSamples = 1.0 / sampleCount;
52 const float tof = 0.5 / float(0x80000000U);
53 uint bits = index;
54 bits = (bits << 16u) | (bits >> 16u);
55 bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
56 bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
57 bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
58 bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
59 return vec2(float(index) * invNumSamples, float(bits) * tof);
60 }
61
62 float DistributionGGX(const float NoH, const float a) {
63 // NOTE: (aa-1) == (a-1)(a+1) produces better fp accuracy
64 float f = (a - 1.0) * ((a + 1.0) * (NoH * NoH)) + 1.0;
65 return (a * a) / (PI * f * f);
66 }
67
68 vec3 hemisphereImportanceSampleDggx(const vec2 u, const float a) { // pdf = D(a) * cosTheta
69 float phi = 2.0 * PI * u.x;
70 // NOTE: (aa-1) == (a-1)(a+1) produces better fp accuracy
71 float cosTheta2 = (1.0 - u.y) / (1.0 + (a + 1.0) * ((a - 1.0) * u.y));
72 float cosTheta = sqrt(cosTheta2);
73 float sinTheta = sqrt(1.0 - cosTheta2);
74 return vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
75 }
76
77 vec4 computeWeight(const uint index, const float roughness) {
78 float sampleCount = materialParams.sampleCount;
79 vec2 u = hammersley(index, sampleCount);
80 vec3 H = hemisphereImportanceSampleDggx(u, roughness);
81 float NoH = H.z;
82 float NoH2 = H.z * H.z;
83 float NoL = saturate(2.0 * NoH2 - 1.0);
84 vec3 L = vec3(2.0 * NoH * H.x, 2.0 * NoH * H.y, NoL);
85 float pdf = DistributionGGX(NoH, roughness) * 0.25;
86 float invOmegaS = sampleCount * pdf;
87 float l = -log4(invOmegaS);
88 return vec4(L, l);
89 }
90
91 float lodToPerceptualRoughness(const float lod) {
92 // Inverse perceptualRoughness-to-LOD mapping:
93 // The LOD-to-perceptualRoughness mapping is a quadratic fit for
94 // log2(perceptualRoughness)+iblMaxMipLevel when iblMaxMipLevel is 4.
95 // We found empirically that this mapping works very well for a 256 cubemap with 5 levels used,
96 // but also scales well for other iblMaxMipLevel values.
97 const float a = 2.0f;
98 const float b = -1.0f;
99 return (lod != 0.0) ? saturate((sqrt(a * a + 4.0 * b * lod) - a) / (2.0 * b)) : 0.0;
100 }
101
102 void postProcess(inout PostProcessInputs postProcess) {
103 vec2 uv = variable_vertex.xy; // interpolated at pixel's center
104
105 float lod = floor(uv.x);
106 uint index = uint(uv.y);
107
108 float perceptualRoughness = lodToPerceptualRoughness(saturate(lod * materialParams.oneOverLevelsMinusOne));
109 float roughness = perceptualRoughness * perceptualRoughness;
110
111 postProcess.weight = computeWeight(index, roughness);
112 }
113
114 }