CV Super Resolution - AMD FSR

Game Super Resolution

Nvidia 是 PC game super resolution 的開創者,開創 DLSS (Deep Learning Super Sampling) 1.0/2.x/3.0. 從名稱就可以知道是 AI 算法。

AMD 也提出一套基於 CV 算法的 SR : CAS $\to$ FSR $\to$ RSR (super resolution).

Intel 也提出 …

以上都是 PC game super resolution running on GPU, 主要的目的是衝 high frame rate. 例如 GPU render native 4K resolution 的 game 因爲硬體算力的限制,可能只能跑到 60 FPS. 藉著使用 super resolution, GPU 只需要 render 1K resolution 的 game 再 up-scale to 4K resolution, 就可以衝到 100 FPS 以上 (+50% 的 frame rate)。

Mediatek 和 Qualcomm 則是利用 super resolution for mobile game. 不過目的不同,主要是穩幀,節省 power. 因爲對於手機功耗是最優先的考量。

  Nvidia AMD Intel Mediatek Qualcomm
Platform PC PC PC Mobile Mobile
Name DLSS1/2/3 CAS/FSR/RSR   AISR SR
Type AI CV AI AI CV
Source Proprietary Open   Proprietary Proprietary
Execution GPU GPU GPU APU GPU?
Purpose 高幀率HFR 高幀率HFR 高幀率HFR 穩幀 穩幀

Recap CV Image Up Scaling 算法

在討論 FSR 之前,我們先 recap CV image up scaling 常見算法 (見前文) = Interpolation + up-sampling.

最常用的 interpolation kernel 是 linear, bicubic 和 Lanczos-2/3 interpolation kernels, 見下圖。

image-20220114224107071

AI vs. CV Image Up Scaling

再來我們先分析 AI 和 CV image up scaling 的 pros and cons. 兩者最基本的差異: AI up-scaling 的 neural network weights 是由 data training 得出,所以完全是 data dependent and determined. CV up-scaling 多半是 fixed formula (e.g. bilinear, bicubic) 和 data 無關;最多有 parameter 可以在 post processing 人爲決定 (e.g. Lanczos-2 or Lanczos-3).

CV image up scaling 最大的問題是 blurred image quality and missing details, 原因是 missing in-band high frequency information (e.g. edge) and attenuated in-band high frequency component (e.g. contrast). 也就是 edge 和 contrast 處理比較不足。剛好這是視覺比較敏感的部分。

  AI image UP Scaling CV image up scaling
Pro Good image quality: sharp edge, more details Low computing complexity, typical with analytical form; flexible input or output image size
Con Fixed input/output image size; high computing complexity Blurred image quality; artifact from aliasing
Issue Need big data for learning Unable to generate missing details

本文主要聚焦 AMD 的 FSR 所使用的 CV 算法。 FSR 特別針對 edge and contrast 加强:

  • EASU(Edge Adaptive Spatial Up Sampling)
  • RCAS(Robust Contrast Adaptive Sharpening)

FSR 1.0 目標

參考 [lottesFidelityFXSuper2021].

  • Provide a dynamic Resolution Scaling solution to reduce frame cost by reducing render resolution (i.e. 1K $\to$ 4K resolution)
  • Easy to integrate, easily portable to anything (iGPU, discrete GPU)
  • No temporal artifacts to the video signal
  • The spatial scaling algorithm
    • Remove resampling artifacts on existing edges during scaling
    • Do not introduce new edges that didn’t exist in the source image

FSR (FidelityFX Super Resolution) 原理

FSR 是 CV 算法,使用 Super Resolution 技術實現高解析度,高品質遊戲畫面,並顯著提高遊戲運行效率的一套實現方法和程序庫。它免費開源,跨平台,針對硬體進行了優化,集成到程序也非常簡單(實際上只有兩個文件),最神奇的是,運行它並不需要特殊的硬體,甚至如前幾代的 Intel CPU with integrated GPU (iGPU),都可以使用該技術。如果現在你在開發一個新的次時代畫面遊戲,真是沒有理由不使用它。

總結一句話:

FSR = 放大 (Up scaling) + 銳化 (Sharpening)

image-20220114222758765

是的,就是如此簡單。在放大的步驟,FSR 實際上通過算法實現了對圖形邊緣 (edge) 的檢測,在放大圖像的同時儘量保持原有的邊緣 (edge),從而實現 SR (Super Resolution)。這呼應前面提到 CV 算法和 data 無關的問題:先用算法 detect edge, 保留 (high frequency) edge information.

往往 CV SR 算法會產生類似模糊 (blurred) 或虛影的錯誤,FSR 使用了一個銳化步驟來消除 CV SR 的這個副作用。可以視爲 sharpen contrast,即是增强 high frequency component.

對 AMD 技術熟悉的朋友大概還記得 AMD 曾經推出過一個銳化技術:Contrast Adaptive Sharpening,簡稱 CAS,沒錯,這裡復用了該技術,並對其進行了針對性優化,稱爲 Robust CAS or RCAS,使得最終的 picture quality 得以保證。

FSR Pipeline

FSR 在 render pipeline 中的位置,位於實際 graphic rendering 完成以後,後處理(Post-processing)之前,你可以把FSR 看做是後處理的一部分,只不過要十分小心 FSR 在後處理中所處的位置,避免一些後處理 導致 FSR 處理錯誤,影響最終的圖像質量。

比如 Film grain 效果,在很多追求電影式畫面的遊戲中廣泛使用,該效果產生的噪點就會影響 FSR 的發揮,FSR 會加強這些噪點的存在感,使得最終畫面出現錯誤。

另外 EASU 和 RCAS 不是硬體,而是執行在 GPU (shader core) 的 software. 實務上分爲兩個 pass. Pass 1: upscaling pass (EASU); Pass 2: sharpening pass.

image-20220114223238192

EASU的工作原理

EASU是 SR (Super Resolution) 的核心。EASU通過優化的採樣策略從原始圖像上取得附近的像素,對其進行插值計算得到目標像素。

除了插值,EASU 還做 edge detection 以及利用 locally adaptive elliptical Lanczos-like filter to performs edge reconstruction.

EASU 算法大致分為四個階段:[happyAMDFSR2021]

Stage 1:Pixel Sampling,像素採樣

在 graphic sampling 是重中之重!

它使用一個圓形的採樣區域來儘量減少採樣的像素,通過特別計算的採樣點,直接利用硬體支持的雙線性採樣 (bilinear sampling?) 函數進行採樣,最大限度降低採樣次數。

Stage 2:Kernel Shaping

Stage 2 and 3 是整個算法中最複雜的一部分,首先是

  • The input frame is analyzed and the main part of the algorithm detects gradient reversals
    • Essentially looking at how neighboring gradients differ – from a set of input pixels.
    • The intensity of the gradient reversals defines the weights to apply to the reconstructed pixels at display resolution.
  • 積累計算線性插值的方向 (Direction) 和長度 (Length)
    • Direction : used to rotate the filter kernel
    • Length: drives post-rotation kernel scaling, and kernel window adjustment

Stage 3:Lanczos-2 Interpolation

  • 然後在所有方向上計算 Lanczos 插值。

此處 FSR 對 Laczos-2算法進行了數值近似,去掉了原有的三角函數和開方運算以提高效率。

Stage 4:Clamp Output: Deringing

由於 Lanczos-2 函數會產生值小於0的部分(見上圖各種 interpolation kernels),在某些情況下會出現 ringing,所以在得到最終結果後,將結果限制在臨近4個像素的最大和最小值之間。

另外,對於支持 16bit 半精度計算的硬體,FSR 使用 packed 16bit 模式,可以使得 2 個 16bit 數據並行計算以提高性能;對於不支持 16bit 半精度的硬體,將回退到 32bit 模式,造成一定的性能損失。

image-20220114225200830

RCAS的工作原理

RCAS 技術是使用像素點附近的局部對比度(Local Contrast)對顏色進行調整,以消除因為抗鋸齒,圖像拉伸等操作造成的細節模糊。

RCAS在此基礎上進行了進一步的優化,去掉了CAS對圖像拉伸的支持(該功能已經由EASU實現了),並且直接使用最大局部銳度進行解算。

image-20220114225523053

由於 FSR 對局部變化比較大(高頻)的區域敏感,所以在 FSR 處理之前圖像不可以有任何添加噪點的後處理操作,如果有必要還應添加抗鋸齒(反走樣)流程。此外 FSA 還提供了一些額外的功能,如下:

  • LFGA (Linear Film Grain Applicator) 用於在縮放圖像後添加 file grain 的算法
  • SRTM (Simple Reversible Tone-Mapper)線性的動態高範圍(HDR)可逆映射器
  • TEPD (Temporal Energy Preserving Dither) 一個將圖像從線性空間轉到Gamma 2.0空間的映射器

具體信息請參考相應的源碼。

應用與發展

FSR的消耗很少,幾年前的硬體都能輕鬆應付,甚至於核心顯卡和行動裝置,都可以運行。相對於其他 Super Resolution 算法來說,這是 FSR 的核心優勢。當然在超高的放大倍數下,例如從 480P $\to$ 4K, FSR可能無法像 AI SR 那樣優秀,但對於 PC Game 來說,Super Resolution 本就是在硬體性能不足的情況下所做的妥協。

另外在 Mobile Game,越來越細膩的高解析度屏幕和長續航時間的要求給遊戲光影渲染帶來很大挑戰,FSR 這種基於數值的方法很可能更適合在移動端大放異彩。

總之,FSR對於玩家和開發者以及硬體廠商來說都是好事情,帶來的收益顯然比超頻要多,而且還不增加能耗,讓玩家手中的顯卡無形中提高了1-2個檔次。

Appendix

主要參考 [lottesFidelityFXSuper2021].

Appendix A: Interpolation Kernel

雖然説是 Lanczos-2 interpolation kernel, 但是 FSR1.0 使用的不完全是標準 Lanczos kernel.

  • 只用 2D 12 點 interpolation, 而不是一般 bicubic or Lanczos-2 的 16 點 interpolation. 這是爲了節省 shader computation.

  • 做 4 次 inner 2x2 ‘+’ pattern 的分析,判斷 edge, 決定 direction and length.

  • 最後是用 locally adaptive elliptical Lanczos-like filter interpolation kernel.

    image-20220116213200081

Appendix B: EASU Analysis

image-20220116213826013

Appendix C: EASU Kernel

  • Use a polynomial approximation instead of calculate sinc function {sin(), sqrt(), rcp?}
  • Two kernels: wide kernel and narrow kernel
    • Wide kernel, window to +/-2 to restore edge => edge uses Lanczos-2 interpolation
    • Narrow kernel, window to +/- sqrt(2) for non-edge => non-edge use more to linear interpolation?

image-20220116002916930

Reference

AMD FSR实现原理分析 - 簡書 (jianshu.com)