Skip to content

Commit

Permalink
PBR: Fixed noise in IBL irradiance cube map (close #200)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailGorobets committed May 14, 2024
1 parent ee0295b commit 1224607
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
13 changes: 6 additions & 7 deletions Shaders/PBR/private/ComputeIrradianceMap.psh
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@ float3 IrradianceMap(float3 N)
// Solid angle of current smple
float OmegaS = 1.0 / (float(g_NumSamples) * pdf);

float NumPixels = g_EnvMapWidth * g_EnvMapHeight;
// Solid angle of 1 pixel across all cube faces
#if ENV_MAP_TYPE == ENV_MAP_TYPE_CUBE
NumPixels *= 6.0;
float OmegaP = ComputeCubeMapPixelSolidAngle(g_EnvMapWidth, g_EnvMapHeight);
#else
// Solid angle of 1 pixel on sphere
float OmegaP = ComputeSphereMapPixelSolidAngle(g_EnvMapWidth, g_EnvMapHeight, acos(L.y), 0.5);
#endif

// Solid angle of 1 pixel across all cube faces
float OmegaP = 4.0 * PI / NumPixels;

// Applying mip bias produces better results, especially for environments maps with
// very bright spots.
float MipBias = 1.0;
float MipLevel = max(0.5 * log2(OmegaS / OmegaP) + MipBias, 0.0);
float MipLevel = max(0.5 * (log2(OmegaS) - log2(OmegaP)) + MipBias, 0.0);
#else
float MipLevel = 0.0;
#endif
Expand Down
12 changes: 12 additions & 0 deletions Shaders/PBR/private/PBR_PrecomputeCommon.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ float3 ImportanceSampleGGX(float2 Xi, float PerceptualRoughness, float3 N)
return TangentX * H.x + TangentY * H.y + N * H.z;
}

float ComputeCubeMapPixelSolidAngle(float Width, float Height)
{
return 4.0 * PI / (6.0 * Width * Height);
}

float ComputeSphereMapPixelSolidAngle(float Width, float Height, float Theta, float Gamma)
{
float dTheta = PI / Width;
float dPhi = 2.0 * PI / Height;
return dPhi * (cos(Theta - 0.5 * dTheta * Gamma) - cos(Theta + 0.5 * dTheta * Gamma));
}

#endif // _PBR_PRECOMPUTE_COMMON_FXH_
14 changes: 7 additions & 7 deletions Shaders/PBR/private/PrefilterEnvMap.psh
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ float3 PrefilterEnvMap( float PerceptualRoughness, float3 R )
// Solid angle of current smple
float OmegaS = 1.0 / (float(g_NumSamples) * pdf);

float NumPixels = g_EnvMapWidth * g_EnvMapHeight;
// Solid angle of 1 pixel across all cube faces
#if ENV_MAP_TYPE == ENV_MAP_TYPE_CUBE
NumPixels *= 6.0;
float OmegaP = ComputeCubeMapPixelSolidAngle(g_EnvMapWidth, g_EnvMapHeight);
#else
// Solid angle of 1 pixel on sphere
float OmegaP = ComputeSphereMapPixelSolidAngle(g_EnvMapWidth, g_EnvMapHeight, acos(L.y), 1.0);
#endif

// Solid angle of 1 pixel across all cube faces
float OmegaP = 4.0 * PI / NumPixels;
// Applying mip bias produces better results, especially for environments maps with
// very bright spots.
float MipBias = 1.0;
float MipLevel = (AlphaRoughness == 0.0) ? 0.0 : max(0.5 * log2(OmegaS / OmegaP) + MipBias, 0.0);
float MipBias = 1.0;
float MipLevel = (AlphaRoughness == 0.0) ? 0.0 : max(0.5 * (log2(OmegaS) - log2(OmegaP)) + MipBias, 0.0);
#else
float MipLevel = 0.0;
#endif
Expand Down

0 comments on commit 1224607

Please sign in to comment.