关于Mali GPU的浮点数异常

一个华为手机上的Bug

今天查一个 辉光抖动 的问题:咱们一个PBR的摩托车,在开辉光后高光处闪烁的厉害,而且这个闪烁只出如今华为手机上(Mali GPU)。git

用RenderDoc分析了一下,闪烁处的高光值已经逆天了,以下图:github

由上图可见,红框标记的颜色值达到了 65504,因为咱们开启了 FP16 HDR,这里的 65504 恰好是 FP16 能表示的最大值。算法

0 11110 1111111111=(-1)^0 * 2^15 * (1+1-2^-10)=65504bash

直觉上这里是 浮点数精度 的问题,由于以前没少吃 Mali GPU 的亏,:)app

修正

要堵这个问题很简单,只须要对最终的高光值用 clamp大法 便可。函数

不过做为一个强迫症患者,我仍是想找到具体是哪里出了问题,因而作了一番调试,最后发现问题代码以下:ui

half perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness);
half roughness = PerceptualRoughnessToRoughness(perceptualRoughness);

half V = SmithJointGGXVisibilityTerm(NoL, NoV, roughness); 
half D = GGXTerm(NoH, roughness);
half specularTerm = V * D * UNITY_PI;
复制代码

这里 PBR 的高光项计算直接摘了Unity的 BRDF1 算法,去掉了 菲涅尔项,上述代码中 roughness精度 影响了最终高光的计算结果。spa

咱们看一下法线分布函数 GGXTerm 的代码:调试

inline float GGXTerm (float NdotH, float roughness)
{
    float a2 = roughness * roughness;
    float d = (NdotH * a2 - NdotH) * NdotH + 1.0f; // 2 mad
    return UNITY_INV_PI * a2 / (d * d + 1e-7f); 
    // This function is not intended to be running on Mobile,
    // therefore epsilon is smaller than what can be represented by half
}
复制代码

参数都是 float,而且函数结尾有一个清楚的注释,说这个函数没打算在移动设备上跑,由于这里 1e-7f 并没考虑兼容 half 的精度:code

This function is not intended to be running on Mobile, therefore epsilon is smaller than what can be represented by half

半精度浮点数能表示的最小值为 6.10×10^(-5)

0 00001 0000000000=2^-14 = 6.10*10^-5

因此把 roughness 的精度从 half 变成 float,这个问题也就修正了。

URP管线对BRDF的简化

在移动设备直接用 Standard管线BRDF1 算法,计算量会略高。

这里咱们也能够参考 BRDF2 的写法,或者参考 URP管线 对于 DirectBDRF 的简化方式,代码以下:

// Based on Minimalist CookTorrance BRDF
// Implementation is slightly different from original derivation: http://www.thetenthplanet.de/archives/255
//
// * NDF [Modified] GGX  
// * Modified Kelemen and Szirmay-Kalos for Visibility term
// * Fresnel approximated with 1/LdotH 
half3 DirectBDRF(BRDFData brdfData, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS)
{
#ifndef _SPECULARHIGHLIGHTS_OFF
    float3 halfDir = SafeNormalize(float3(lightDirectionWS) + float3(viewDirectionWS)); 

    float NoH = saturate(dot(normalWS, halfDir));
    half LoH = saturate(dot(lightDirectionWS, halfDir));

    // GGX Distribution multiplied by combined approximation of Visibility and Fresnel 
    // BRDFspec = (D * V * F) / 4.0
    // D = roughness^2 / ( NoH^2 * (roughness^2 - 1) + 1 )^2 
    // V * F = 1.0 / ( LoH^2 * (roughness + 0.5) )
    // See "Optimizing PBR for Mobile" from Siggraph 2015 moving mobile graphics course
    // https://community.arm.com/events/1155

    // Final BRDFspec = roughness^2 / ( NoH^2 * (roughness^2 - 1) + 1 )^2 * (LoH^2 * (roughness + 0.5) * 4.0)
    // We further optimize a few light invariant terms 
    // brdfData.normalizationTerm = (roughness + 0.5) * 4.0 rewritten as roughness * 4.0 + 2.0 to a fit a MAD. 
    float d = NoH * NoH * brdfData.roughness2MinusOne + 1.00001f;

    half LoH2 = LoH * LoH;    
    half specularTerm = brdfData.roughness2 / ((d * d) * max(0.1h, LoH2) * brdfData.normalizationTerm);

    // On platforms where half actually means something, the denominator has a risk of overflow
    // clamp below was added specifically to "fix" that, but dx compiler (we convert bytecode to metal/gles)
    // sees that specularTerm have only non-negative terms, so it skips max(0,..) in clamp (leaving only min(100,...))
#if defined (SHADER_API_MOBILE) || defined (SHADER_API_SWITCH)
    specularTerm = specularTerm - HALF_MIN;
    specularTerm = clamp(specularTerm, 0.0, 100.0); // Prevent FP16 overflow on mobiles
#endif

    half3 color = specularTerm * brdfData.specular + brdfData.diffuse;
    return color;
#else
    return brdfData.diffuse;  
#endif
}
复制代码

代码注释写得很清楚,简化方式参考了 SIGGRAPH 2015Optimizing PBR for Mobile

经典的微表面高光 BRDF 公式以下:

按照 Optimizing PBR for Mobile 的方式,能够对 V * F 合并和近似:

BRDFspec = (D * V * F) / 4.0

D = roughness^2 / ( NoH^2 * (roughness^2 - 1) + 1 )^2

V * F = 1.0 / ( LoH^2 * (roughness + 0.5) )

最终结果以下:

最后,上面的代码也兼顾了 half 的精度:

#define HALF_MIN 6.103515625e-5 // 2^-14, the same value for 10, 11 and 16-bit: https://www.khronos.org/opengl/wiki/Small_Float_Formats

// On platforms where half actually means something, the denominator has a risk of overflow
// clamp below was added specifically to "fix" that, but dx compiler (we convert bytecode to metal/gles)
// sees that specularTerm have only non-negative terms, so it skips max(0,..) in clamp (leaving only min(100,...))

#if defined (SHADER_API_MOBILE) || defined (SHADER_API_SWITCH)
    specularTerm = specularTerm - HALF_MIN;
    specularTerm = clamp(specularTerm, 0.0, 100.0); // Prevent FP16 overflow on mobiles
#endif
复制代码

我的主页

本文的我的主页连接:baddogzz.github.io/2020/04/27/…

好了,拜拜!

相关文章
相关标签/搜索