Skip to content

Corrupted rendering of specular roughness for shading modules on Android #2654

@kwokcb

Description

@kwokcb

Issue

On Android devices if you set the specular roughness low enough, you appear to get solid white pixels in the areas facing the primary light direction which I believe is computed based on the light environment map.

This occurs for all shading models so appears to be a common math issue.

This does not occur on Apple devices.

Triage

  • Around 0.07 for glTF and 0.08 for OpenPBR and standard surface causes this with all other input values the default.
  • Code is ESSL with mediump precision setting.

After hunting around a guess as to areas with issues are the following:

// FIX 1: Add safety to GGX Smith G1 function
float mx_ggx_smith_G1(float cosTheta, float alpha)
{
    float cosTheta2 = mx_square(cosTheta);
    float tanTheta2 = (1.0 - cosTheta2) / max(cosTheta2, 1e-6); // PREVENT DIVISION BY ZERO
    float alpha2 = mx_square(max(alpha, 0.001)); // ENFORCE MINIMUM ALPHA
    return 2.0 / (1.0 + sqrt(1.0 + alpha2 * tanTheta2));
}

// FIX 2: Add safety to GGX Smith G2 function  
float mx_ggx_smith_G2(float NdotL, float NdotV, float alpha)
{
    float alpha2 = mx_square(max(alpha, 0.001)); // ENFORCE MINIMUM ALPHA
    
    // ADD SAFETY CHECKS FOR NEAR-ZERO DOT PRODUCTS
    NdotL = max(NdotL, 1e-6);
    NdotV = max(NdotV, 1e-6);
    
    float lambdaL = sqrt(alpha2 + (1.0 - alpha2) * mx_square(NdotL));
    float lambdaV = sqrt(alpha2 + (1.0 - alpha2) * mx_square(NdotV));
    
    // PREVENT DIVISION BY ZERO
    float denominator = lambdaL * NdotV + lambdaV * NdotL;
    return 2.0 * NdotL * NdotV / max(denominator, 1e-8);
}

// FIX 3: Add safety to the main BSDF response calculation
// In mx_dielectric_bsdf and mx_generalized_schlick_bsdf, find this line:
bsdf.response = D * F * G * comp * safeTint * closureData.occlusion * weight / (4.0 * NdotV);

// CHANGE TO:
float denominator = 4.0 * max(NdotV, 1e-6);
bsdf.response = D * F * G * comp * safeTint * closureData.occlusion * weight / denominator;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions