說一下 Deferred Shading MSAA那些事

以下內容純屬自己總結如有錯誤歡迎指正

前言 defferred shading 可以在大量光照場景中節省性能,但是不能使用混合不能使用 MSAA。

下面先梳理一下概念

MSAA 

多重採樣抗鋸齒 與普通採樣不一樣的地方是 在光柵化階段,由硬件自己進行特定運算來多重採樣最終輸出平滑的顏色

wiki 定義如下

In supersample anti-aliasing, multiple locations are sampled within every pixel, and each of those samples is fully rendered and combined with the others to produce the pixel that is ultimately displayed. This is computationally expensive, because the entire rendering process must be repeated for each sample location. It is also inefficient, as aliasing is typically only noticed in some parts of the image, such as the edges, whereas supersampling is performed for every single pixel.

在超級採樣中,在每一個像素的多個位置進行採樣(比如 上下左右角還有中間等),並且這些採樣被完全渲染,並且與其他樣本結合起來生成最終的圖像,這個計算很昂貴,因爲這些採樣位置必須都要進行處理。對於僅僅某些部分有鋸齒的圖片這樣處理這顯然不是高效的。

In multisample anti-aliasing, if any of the multi sample locations in a pixel is covered by the triangle being rendered, a shading computation must be performed for that triangle. However this calculation only needs to be performed once for the whole pixel regardless of how many sample positions are covered; the result of the shading calculation is simply applied to all of the relevant multi sample locations.

在多重採樣中,如果每一個三角形的覆蓋一個像素的多個位置的任意一個,一個着色就算就必須執行。然而不管多少採樣位置被覆蓋每一個像素僅僅執行一次着色計算。這樣的着色計算結果就會被應用在所有的多重採樣位置中。

In the case where only one triangle covers every multi sample location within the pixel, only one shading computation is performed, and these pixels are little more expensive (and the result is no different) than in the non-anti-aliased image. This is true of the middle of triangles, where aliasing is not an issue. (Edge detection can reduce this further by explicitly limiting the MSAA calculation to pixels whose samples involve multiple triangles, or triangles at multiple depths.) In the extreme case where each of the multi sample locations is covered by a different triangle, a different shading computation will be performed for each location and the results then combined to give the final pixel, and the result and computational expense are the same as in the equivalent supersampled image.

The shading calculation is not the only operation that must be performed on a given pixel; multisampling implementations may variously sample other operations such as visibility at different sampling levels.

對於沒有鋸齒的圖片來說,採用三角形覆蓋多個採樣點,在每一個像素中僅僅計算一次也是有點昂貴的。這對於三角形中間沒有鋸齒問題來說肯定是的(邊緣檢測技術能夠很大程度減少MSAA的對於三角形覆蓋採樣問題,或者是多個三角形具有多個深度問題(也就是說三角形不在一個平面上)在這種極端的情況下,每一個三角形的多重採樣採樣都會進行計算最終混合得出結果,這就會導致這個計算跟超級採樣一樣昂貴。

着色計算並不是必須在給定像素上執行的唯一操作;多采樣實現可以對其他操作進行不同的採樣,例如在不同的採樣級別上的可見性。(感覺這段話理解很有問題)

優點:每一個像素着色僅僅需要計算一次        支持幾何邊緣抗鋸齒

缺點:

alpha testing

由於alpha測試時根據物體的alpha值來剔除一些不滿足alpha特定值的一些像素,由於是在像素級別進行剔除,所以對像素進行採樣肯定會出現一些鋸齒,即使不是在邊緣也會出現。

總結一下:

其實這個多重採樣描述不太準確,應該叫多個採樣點(這個多個採樣點是對於超級採樣的多個才樣點來說的)進行一次採樣。而且通過光柵化三角形覆蓋的像素來對一個像素進行一次計算的採樣,缺點不支持alpha測試。附帶一張圖片進行說明如果想仔細瞭解的話參考 

離屏MSAA渲染 

摘抄一段概念

首先我們需要生成多重採樣緩衝。有兩種方式可以創建多重採樣緩衝,將其作爲幀緩衝的附件:紋理附件和渲染緩衝附件。渲染到多重採樣幀緩衝對象的過程都是自動的。只要我們在幀緩衝綁定時繪製任何東西,光柵器就會負責所有的多重採樣運算。我們最終會得到一個多重採樣顏色緩衝以及/或深度和模板緩衝。因爲多重採樣緩衝有一點特別,我們不能直接將它們的緩衝圖像用於其他運算,比如在着色器中對它們進行採樣。

多重採樣的圖像包含比普通圖像更多的信息,我們所要做的是縮小或者還原(Resolve)圖像。多重採樣幀緩衝的還原通常是通過glBlitFramebuffer來完成,它能夠將一個幀緩衝中的某個區域複製到另一個幀緩衝中,並且將多重採樣緩衝還原

但如果我們想要使用多重採樣幀緩衝的紋理輸出來做像是後期處理這樣的事情呢?我們不能直接在片段着色器中使用多重採樣的紋理。但我們能做的是將多重採樣緩衝位塊傳送到一個沒有使用多重採樣紋理附件的FBO中。然後用這個普通的顏色附件來做後期處理,從而達到我們的目的。然而,這也意味着我們需要生成一個新的FBO,作爲中介幀緩衝對象,將多重採樣緩衝還原爲一個能在着色器中使用的普通2D紋理

defferred shadingng 延遲着色 區別於forward shading 的地方在於在許多光照下 我們在每個片段只會運行一次着色(所以不能使用混合,因爲混合需要多個片段,而我們最終存儲的只有唯一的一個位置的一個片段信息)forward shading 其實就是 每一個片段對每一個光照都會運行一次着色,defferred shaing是每個光照對每一個片段運行一次。所以,如果光照多使用defferred shading 省性能。會使用兩個pass:第一個pass叫幾何渲染 第二個是光照渲染。

幾何渲染 得到當前場景中所有物體的信息包括世界空間位置、法線、 顏色、深度信息等到一個叫做GBuffer的東西中,由於這個信息量巨大所以需要多重渲染目標(MRT)即渲染到多張浮點紋理中。這個過程是普通的渲染流程。

第二個pass 光照渲染 這個過程就比較特殊 我們可以直接通過在幾何渲染過程中得到的GBuffer緩衝信息來對光照進行計算最終輸出顏色。下面這個圖可以抽象的描述



下面來說說爲什麼在defferred shading 中不能直接使用MSAA。

首先由於defferred shading 有兩個pass,所以直接通過多重紋理採樣來實現已經不行了。那麼離屏渲染呢,咋一看可以離屏渲染需要使用MRT,而Defferred 也需要,這不是正好嗎,但是離屏渲染條件(上面黃色黑背景的文字)由於我們已經有一個GBuffer了,即使我們可以再創建一個幀緩衝但是這個緩衝保存到哪裏呢,即使可以保存但是多重採樣也是才光柵化階段自己處理的,沒有兩個buffer也不能同時在光柵化階段處理,所以在defferred shading 中不能使用MSAA。即使能實現代價也是巨大的除非我們可以同時處理兩個 fram buffer,只能等待硬件的發展了。

參考  https://learnopengl.com/Advanced-OpenGL/Anti-Aliasing

        https://en.wikipedia.org/wiki/Multisample_anti-aliasing



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章