转载请注明出处:https://zhuanlan.zhihu.com/p/130428432
说明
这是我在学习压缩格式(HDR格式,RGBE编码)时突发奇想,想到的一个优化思路,我估计游戏厂商也早已有这方面经验积累,但资料略少,就整理了一些。
渲染优化,除了场景管理,LOD,管线优化,裁剪,分辨率,mipmaps等,计算上也可以优化。本文主要列举计算中常用到的耗时的公式的优化。
公式优化的思路就是若库函数效率低下就用特化版本或者近似版本。需要慢慢积累,形成一个快速计算数学库。
内容多来自IDTech6、7和github的一个开源库
https://github.com/michaldrobot/ShaderFastLibs/blob/master/ShaderFastMathLib.h
我重新整理了一份,新增了若干项。放在github上:
https://github.com/ifeuille/FastMathTests/blob/master/ShaderFastMathLib.h
前置计算机或数学基础知识关键词:
ALU、NewtonRaphson迭代、IEEE
特化
1.pow(x)
直接特化对应版本
比如
float
近似
pow(x)
还是pow,这是idtech6、7里看到的。
float
log2(x)
这是在idtech6、7里看到的
float
exp2(x)
这是在idtech6、7里看到的
float
RCP
// RCP
基于NR的sqrt
//基于IEEE浮点整数浮点数算法的近似值计算
acos
4阶多项式逼近
4 VGRP,16 ALU
7*10^-5弧度精度
资料:Handbook of Mathematical Functions (chapter : Elementary Transcendental Functions), M. Abramowitz and I.A. Stegun, Ed.
float
asin
4阶多项式逼近
4 VGRP,16 ALU
7*10^-5弧度精度
float
atan
4阶多项式逼近
4 VGRP,12 ALU
7*10^-5弧度精度
资料:Efficient approximations for the arctangent function, Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006
float
资料
https://github.com/michaldrobot/ShaderFastLibs/blob/master/ShaderFastMathLib.h
Efficient approximations for the arctangent function, Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006
Handbook of Mathematical Functions (chapter : Elementary Transcendental Functions), M. Abramowitz and I.A. Stegun, Ed.
https://github.com/ifeuille/FastMathTests/blob/master/ShaderFastMathLib.h