交換鏈結Swap chains

交換鏈結是用來向使用者顯示畫面的緩衝區集合。A swap chain is a collection of buffers that are used for displaying frames to the user. 每次應用程式提供新畫面供顯示時,交換鏈結中的第一個緩衝區會取代顯示之緩衝區的位置。Each time an application presents a new frame for display, the first buffer in the swap chain takes the place of the displayed buffer. 這個程序稱為 「交換」「翻轉」This process is called swapping or flipping.

圖形卡會保留表面的指標,表示監視器上顯示之影像,稱為前端緩衝區。A graphics adapter holds a pointer to a surface that represents the image being displayed on the monitor, called a front buffer. 當監視器重新整理時,圖形卡會將前端緩衝區的內容傳送到監視器上顯示。As the monitor is refreshed, the graphics card sends the contents of the front buffer to the monitor to be displayed. 不過,這在轉譯即時圖形時會導致「撕裂」問題。However, this leads to a "tearing" problem when rendering real-time graphics. 此問題的核心是,相較於電腦的其他部分,監視器重新整理頻率非常慢。The heart of the problem is that monitor refresh rates are very slow in comparison to the rest of the computer. 一般重新整理頻率範圍從 60 Hz(每秒 60 次)到 100 Hz。Common refresh rates range from 60 Hz (60 times per second) to 100 Hz.

如果在監視器重新整理進行時,您的應用程式更新前端緩衝區,顯示的影像會被切成一半,上半部顯示包含舊的影像,下半部包含新影像。If your application is updating the front buffer while the monitor is in the middle of a refresh, the image that is displayed will be cut in half with the upper half of the display containing the old image and the lower half containing the new image. 這個問題稱為 「撕裂」This problem is referred to as tearing.

避免撕裂Avoiding tearing

Direct3D 實作兩個選項來避免撕裂:Direct3D implements two options to avoid tearing:

  • 一個選項只允許垂直折返(或垂直同步)作業的監視器更新。An option to only allow updates of the monitor on the vertical retrace (or vertical sync) operation. 監視器通常藉由水平移動光針來重新整理其影像,從監視器左上角開始以 Z 字形移至監視器右下方。A monitor typically refreshes its image by moving a light pin horizontally, zigzagging from the top left of the monitor and ending at the bottom right. 當光針到達底部時,監視器會將光針移回至左上角,重新校正光針,讓此程序可以重新開始。When the light pin reaches the bottom, the monitor recalibrates the light pin by moving it back to the upper left so that the process can start again.

    這個重新校正稱為垂直同步。在垂直同步時,監視器不繪製任何項目,所以將不會看到前端緩衝區的任何更新,直到監視器重新繪製為止。This recalibration is called a vertical sync. During a vertical sync, the monitor is not drawing anything, so any update to the front buffer will not be seen until the monitor starts to draw again. 垂直同步相當慢。不過,不會太慢而無法在等待時轉譯複雜的場景。The vertical sync is relatively slow; however, not slow enough to render a complex scene while waiting. 避免撕裂所需,並且可以呈現複雜場景的程序,稱為背景緩衝。What is needed to avoid tearing and be able to render complex scenes is a process called back buffering.

  • 一個選項使用稱為背景緩衝的技術。An option to use a technique called back buffering. 背景緩衝是將場景繪製到螢幕外表面(稱為背景緩衝區)的程序。Back buffering is the process of drawing a scene to an off-screen surface, called a back buffer. 前端緩衝區以外的任何表面都稱為螢幕外表面,因為監視器永遠不會直接檢視它。Any surface other than the front buffer is called an off-screen surface because it is never directly viewed by the monitor.

    使用背景緩衝區,應用程式有自由在系統閒置時(也就是沒有正在等待的 Windows 訊息)呈現場景,而不需要考慮監視器的重新整理頻率。By using a back buffer, an application has the freedom to render a scene whenever the system is idle (that is, no windows messages are waiting) without having to consider the monitor's refresh rate. 背景緩衝增加了如何及何時將背景緩衝區移至前端緩衝區的複雜度。Back buffering brings in an additional complication of how and when to move the back buffer to the front buffer.

介面翻轉Surface flipping

將背景緩衝區移至前端緩衝區的程序,稱為表面翻轉。The process of moving the back buffer to the front buffer is called surface flipping. 因為圖形卡只是使用表面指標來代表前端緩衝區,所以只需要簡單指標變更,即可將背景緩衝區設至前端緩衝區。Because the graphics card simply uses a pointer to a surface to represent the front buffer, a simple pointer change is all that is needed to set the back buffer to the front buffer. 當應用程式要求 Direct3D 將背景緩衝區呈現到前端緩衝區時,Direct3D 只是「翻轉」兩個表面指標。When an application asks Direct3D to present the back buffer to the front buffer, Direct3D simply "flips" the two surface pointers. 結果是背景緩衝區現在是新的前端緩衝區,而舊的前端緩衝區現在是新的背景緩衝區。The result is that the back buffer is now the new front buffer, and the old front buffer is the new back buffer.

當應用程式要求 Direct3D 裝置呈現背景緩衝區時,就會叫用表面翻轉;不過,Direct3D 可以設定為佇列要求,直到發生垂直同步為止。A surface flip is invoked whenever an application asks the Direct3D device to present the back buffer; however, Direct3D can be set up to queue the requests until a vertical sync occurs. 這個選項稱為 Direct3D 裝置的呈現間隔。This option is referred to as the Direct3D device's presentation interval. 根據應用程式指定 Direct3D 應如何處理表面翻轉,新的背景緩衝區中的資料可能無法重複使用。The data in the new back buffer may not be reusable, depending on how an application specifies how Direct3D should handle surface flipping.

表面翻轉是多媒體、動畫和遊戲軟體的關鍵;它相當於使用一疊紙產生動畫的方式。Surface flipping is key in multimedia, animation, and game software; it is equivalent to the way you can do animation with a pad of paper. 在每頁上,畫家將人物稍微改變,所以當您快速翻轉紙張時繪圖看似動畫。On each page, the artist changes the figures slightly, so that when you flip rapidly between sheets, the drawing appears animated.

相關主題Related topics

裝置Devices