diff --git a/res/shaders/EnvironmentMapping.slang b/res/shaders/EnvironmentMapping.slang index 05015e6..438fbf2 100644 --- a/res/shaders/EnvironmentMapping.slang +++ b/res/shaders/EnvironmentMapping.slang @@ -123,4 +123,70 @@ float4 convolveCubemap(float3 localPos : LOCALPOS) : SV_Target irradiance = PI * irradiance * (1.0 / nrSamples); return float4(irradiance, 1); +} + +float RadicalInverse_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 +} + +float2 Hammersley(uint i, uint N) +{ + return float2(float(i)/float(N), RadicalInverse_VdC(i)); +} + +float3 ImportanceSampleGGX(float2 Xi, float3 N, float roughness) +{ + float a = roughness*roughness; + + float phi = 2.0 * PI * Xi.x; + float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a*a - 1.0) * Xi.y)); + float sinTheta = sqrt(1.0 - cosTheta*cosTheta); + + // from spherical coordinates to cartesian coordinates + float3 H; + H.x = cos(phi) * sinTheta; + H.y = sin(phi) * sinTheta; + H.z = cosTheta; + + // from tangent-space vector to world-space sample vector + float3 up = abs(N.z) < 0.999 ? float3(0.0, 0.0, 1.0) : float3(1.0, 0.0, 0.0); + float3 tangent = normalize(cross(up, N)); + float3 bitangent = cross(N, tangent); + + float3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z; + return normalize(sampleVec); +} + +[shader("pixel")] +float4 computePrefilteredCubemap(float3 localPos : LOCALPOS) : SV_Target +{ + float3 N = normalize(localPos); + float3 R = N; + float3 V = R; + + const uint SAMPLE_COUNT = 1024u; + float totalWeight = 0.0; + float3 prefilteredColor = float3(0.0); + for(uint i = 0u; i < SAMPLE_COUNT; ++i) + { + float2 Xi = Hammersley(i, SAMPLE_COUNT); + float3 H = ImportanceSampleGGX(Xi, N, roughness); + float3 L = normalize(2.0 * dot(V, H) * H - V); + + float NdotL = max(dot(N, L), 0.0); + if(NdotL > 0.0) + { + prefilteredColor += pViewParams.cubeMap.Sample(pViewParams.sampler, L).rgb * NdotL; + totalWeight += NdotL; + } + } + prefilteredColor = prefilteredColor / totalWeight; + + return float4(prefilteredColor, 1.0); } \ No newline at end of file diff --git a/res/shaders/lib/LightEnv.slang b/res/shaders/lib/LightEnv.slang index c3d6017..11f6b74 100644 --- a/res/shaders/lib/LightEnv.slang +++ b/res/shaders/lib/LightEnv.slang @@ -69,6 +69,9 @@ struct LightEnv uint numPointLights; TextureCube irradianceMap; SamplerState irradianceSampler; + TextureCube prefilteredMap; + SamplerState prefilteredSampler; + Texture2D brdfLUT; }; layout(set=3) ParameterBlock pLightEnv; @@ -294,9 +297,9 @@ struct CookTorrance : IBRDF return ggx1 * ggx2; } - float3 FresnelSchlick(float cosTheta, float3 F0) + float3 FresnelSchlickRoughness(float cosTheta, float3 F0, float roughness) { - return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); + return F0 + (max(float3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0, 1), 5.0); } float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor) @@ -309,7 +312,7 @@ struct CookTorrance : IBRDF float NDF = TrowbridgeReitzGGX(n, h); float G = Smith(n, viewDir_WS, lightDir_WS); - float3 F = FresnelSchlick(max(dot(h, viewDir_WS), 0.0), F0); + float3 F = FresnelSchlickRoughness(max(dot(h, viewDir_WS), 0.0), F0, roughness); float3 num = NDF * G * F; float denom = 4.0 * max(dot(n, viewDir_WS), 0.0) * max(dot(n, lightDir_WS), 0.0) + 0.000001; @@ -337,7 +340,7 @@ struct CookTorrance : IBRDF { float3 F0 = float3(0.04); F0 = lerp(F0, baseColor, metallic); - float3 k_s = FresnelSchlick(max(dot(normal, viewDir_WS), 0.0), F0); + float3 k_s = FresnelSchlickRoughness(max(dot(normal, viewDir_WS), 0.0), F0, roughness); float3 k_d = 1 - k_s; k_d *= 1 - metallic; float3 irradiance = pLightEnv.irradianceMap.Sample(pLightEnv.irradianceSampler, normal).rgb;