先贴一个UE4 TAA的slide
https://de45xmedrsdbp.cloudfront.net/Resources/files/TemporalAA_small-59732822.pdf

里面细节问题很多,先记录一下目前想到和遇到的问题,便于备忘,后面有空的话再记录。

TAA用到的Velocity和抖动对精度要求比较高, 特别是大场景下误差容易比较大,UE4做了一系列的处理来保持精度。

投影矩阵的精度

     static const FMatrix InvertProjectionMatrix( const FMatrix& M )
{
if( M.M[][] == 0.0f &&
M.M[][] == 0.0f &&
M.M[][] == 0.0f &&
M.M[][] == 0.0f &&
M.M[][] == 0.0f &&
M.M[][] == 0.0f &&
M.M[][] == 0.0f &&
M.M[][] == 0.0f &&
M.M[][] == 1.0f &&
M.M[][] == 0.0f )
{
// Solve the common case directly with very high precision.
/*
M =
| a | 0 | 0 | 0 |
| 0 | b | 0 | 0 |
| s | t | c | 1 |
| 0 | 0 | d | 0 |
*/ double a = M.M[][];
double b = M.M[][];
double c = M.M[][];
double d = M.M[][];
double s = M.M[][];
double t = M.M[][]; return FMatrix(
FPlane( 1.0 / a, 0.0f, 0.0f, 0.0f ),
FPlane( 0.0f, 1.0 / b, 0.0f, 0.0f ),
FPlane( 0.0f, 0.0f, 0.0f, 1.0 / d ),
FPlane( -s/a, -t/b, 1.0f, -c/d )
);
}
else
{
return M.Inverse();
}
}

可以看到对投影矩阵的取反做了特殊处理,来提高浮点计算精度。

视图矩阵的精度

     FVector DeltaTranslation = InPrevViewMatrices.GetPreViewTranslation() - InViewMatrices.GetPreViewTranslation();
FMatrix InvViewProj = InViewMatrices.ComputeInvProjectionNoAAMatrix() * InViewMatrices.GetTranslatedViewMatrix().GetTransposed();
FMatrix PrevViewProj = FTranslationMatrix(DeltaTranslation) * InPrevViewMatrices.GetTranslatedViewMatrix() * InPrevViewMatrices.ComputeProjectionNoAAMatrix(); ViewUniformShaderParameters.ClipToPrevClip = InvViewProj * PrevViewProj;

将视图矩阵的位移和旋转拆开来(T*R),以避免大地图上,相机位置过大造成的误差。

Reprojection是把当前帧Clip space或NDC space的点重新投影到上一帧的位置

Reprojection = (V*P)-1 * (PrevV*PrevP)

=  P-1 * V-1 * PrevV * PrevP

其中V为视图矩阵,P为投影矩阵。

而UE4的视图矩阵实际使用TR的方式 (类似变换矩阵的TRS和RTS的顺序,区别是试图矩阵没有缩放, https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/why-transformation-order-is-significant

注意如果是相同的T和R, T*R和R*T结果是不一样的,这里的TR,是在ViewMatrix结果一样的前提上,拆解出的另外两个矩阵,从几何分析的角度也可以得出)

Reprojection = P-1 * (T*R)-1 * (PrevT*PrevR) * prevP

= P-1 * R-1 * T-1 * PrevT * PrevR * PrevP

= P-1 * R-1 * (T-1 * PrevT) * PrevR * PrevP

其中:
Reprojection是UE4代码里的ViewUniformShaderParameters.ClipToPrevClip,
R是ViewMatrix的旋转部分(UE4代码中的GetTranslatedViewMatrix),
T是ViewMatrix的位移部分,

T-1*PrevT 就是UE4代码中的 FTranslationMatrix(DeltaTranslation) 。

这样做的好处是,把两个绝对位置转换为一个相对位移, 从而避免绝对位置过大而导致的矩阵Inverse的精度问题。

同时,视图矩阵的旋转部分 R 是正交矩阵,所以Transpose等价于Inverse,这样不仅是效率的提高,更重要的是避免了Inverse导致的浮点误差。

VelocityBuffer的精度

 // for velocity rendering, motionblur and temporal AA
// velocity needs to support -2..2 screen space range for x and y
// texture is 16bit 0..1 range per channel
float2 EncodeVelocityToTexture(float2 In)
{
// 0.499f is a value smaller than 0.5f to avoid using the full range to use the clear color (0,0) as special value
// 0.5f to allow for a range of -2..2 instead of -1..1 for really fast motions for temporal AA
return In * (0.499f * 0.5f) + 32767.0f / 65535.0f;
}
// see EncodeVelocityToTexture()
float2 DecodeVelocityFromTexture(float2 In)
{
const float InvDiv = 1.0f / (0.499f * 0.5f);
// reference
// return (In - 32767.0f / 65535.0f ) / (0.499f * 0.5f);
// MAD layout to help compiler
return In * InvDiv - 32767.0f / 65535.0f * InvDiv;
}

VelocityBuffer在TAA里用来reproject移动的物体,包括蒙皮动画和位移旋转动画等。

为了提高精度,VelocityBuffer可以使用Float32。Unity使用的是Float16x2 (R16G16F), 不同的是,UE4使用的是INT16x2 (R16G16_INT)。
对于Float16,最小精度是2-10。 如果映射到Int16,去掉等价的符号位,精度是2-15。 这样在占用同样显存大小的情况下,提高了精度。

[工作积累] UE4 TAA ReProjection的精度处理的更多相关文章

  1. [工作积累] UE4 并行渲染的同步 - Sync between FParallelCommandListSet & FRHICommandListImmediate calls

    UE4 的渲染分为两个模式1.编辑器是同步绘制的 2.游戏里是FParallelCommandListSet并行派发的. mesh渲染也分两类,static mesh 使用TStaticMeshDra ...

  2. [工作积累] TAA Ghosting 的相关问题

    因为TAA要使用上一帧的历史结果,那么在相机移动的时候,颜色就会有残留,出现ghosting(残影). 由于上一帧历史是累积的,是由上一帧的直接渲染结果和上上帧的结果做了合并,所以ghosting并不 ...

  3. [工作积累] Tricks with UE4 PerInstanceRandom

    最近在用UE4的Instancing, 发现限制很多. Unity有instancing的attribute array (uniform/constant buffer),通过InstanceID来 ...

  4. [工作积累] D3D10+ 中 Pixel Shader 的input semantic和参数顺序

    由于semantic的使用,我们有理由相信 vertex shader的output 和 pixel shader的input是按照semantic来匹配的,而跟传入顺序无关.印象dx9时代是这样. ...

  5. [工作积累] shadow map问题汇总

    1.基本问题和相关 Common Techniques to Improve Shadow Depth Maps: https://msdn.microsoft.com/en-us/library/w ...

  6. [工作积累] Google/Amazon平台的各种坑

    所谓坑, 就是文档中没有标明的特别需要处理的细节, 工作中会被无故的卡住各种令人恼火的问题. 包括系统级的bug和没有文档化的限制. 继Android的各种坑后, 现在做Amazon平台, 遇到的坑很 ...

  7. [工作积累] 32bit to 64bit: array index underflow

    先贴一段C++标准(ISO/IEC 14882:2003): 5.2.1 Subscripting: 1 A postfix expression followed by an expression ...

  8. [工作积累] bitfield

    ISO/IEC 14882:2003: 9.6 Bit-fields [class.bit] A member-declarator of the form identifieropt : const ...

  9. [工作积累] GCC 4.6 new[] operator内存对齐的BUG

    对于用户没有定义dctor(包括其所有成员)的类来说, new CLASS[n] 可能会直接请求sizeof(CLASS)*n的空间. 而带有dctor的 类, 因为delete[]的时候要逐个调用析 ...

随机推荐

  1. egret 简单的四方向的a星寻路,在wing中可以直接跑

    /** * main类中加载场景 * 创建游戏场景 * Create a game scene */ private createGameScene() { MtwGame.Instance.init ...

  2. 如何查看tomcat的支持的jdk、servlet、jsp的版本

    解压servlet-api 查看 可以看出,支持的servlet版本是4.0,jdk是1.8

  3. Problem A: 字符的变化

    Description 定义一个Character类,具有: 1. char类型的数据成员. 2.构造函数Character(char). 3. Character toUpper():如果当前字符是 ...

  4. java基础知识—数组

    1.数组:是一个变量,存储相同数据类型的一组数据. 2.数据的优点:减少代码量.易查找. 3.数组的使用步骤: 1)声明数组:int scores []: 2)开辟空间:scores = new in ...

  5. git bash + gitee

    使用Git Bash从Gitee上下载代码到本地以及上传代码到码云Git: https://www.cnblogs.com/babysbreath/p/7274195.html 指定克隆远端分支 ht ...

  6. Linux常用命令之Yum

    Linux Yum命令详解Yum全称Yellow dog Updater,Modified,是一个在Fedora和RedHat以及SUSE中提供的基于RPM包的软件包管理工具,能够从指定的服务器自动下 ...

  7. Holer实现外网访问ARM嵌入式Linux系统

    公网访问ARM嵌入式Linux系统 实验室里的ARM嵌入式Linux系统,只能在局域网内访问,怎样从公网也能访问内网ARM嵌入式Linux系统? 本文将介绍使用holer实现的具体步骤. 1. 准备工 ...

  8. Git bash命令

    1.最开始使用git的时候, git remote -v 查看远程仓库 报了一个错误fatal: not a git repository (or any of the parent director ...

  9. java ftp上传文件 工具类

    package com.learning.spboot.utils; import com.jcraft.jsch.*; import org.apache.commons.net.ftp.FTPCl ...

  10. FCC JS基础算法题(5):Return Largest Numbers in Arrays(找出多个数组中的最大数)

    题目描述: 找出多个数组中的最大数右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组.提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组 ...