從 Direct3D 9 到 Direct3D 11 的重要變更Important changes from Direct3D 9 to Direct3D 11

總結Summary

本主題說明 DirectX 9 和 DirectX 11 的概要差異。This topic explains the high-level differences between DirectX 9 and DirectX 11.

Direct3D 11 基本上與 Direct3D 9 使用相同的 API 類型,也就是圖形硬體中的低階虛擬化介面;Direct3D 11 is fundamentally the same type of API as Direct3D 9 - a low-level, virtualized interface into graphics hardware. 但它還是可以讓您在各種硬體實作上執行繪圖操作。It still allows you to perform graphics drawing operations on a variety of hardware implementations. 圖形 API 的配置從 Direct3D 9 開始就已經變更,不但擴充了裝置內容的概念,也特別針對圖形基礎結構新增 API。The layout of the graphics API has changed since Direct3D 9; the concept of a device context has been expanded, and an API has been added specifically for graphics infrastructure. 儲存在 Direct3D 裝置上的資源有一個全新的資料多型機制,稱為資源檢視。Resources stored on the Direct3D device have a novel mechanism for data polymorphism called a resource view.

核心 API 函式Core API functions

過去在 Direct3D 9,您必須先建立 Direct3D API 的介面才能開始使用它。In Direct3D 9 you had to create an interface to the Direct3D API before you could start using it. 而在 Direct3D 11 通用 Windows 平台 (UWP) 遊戲中,您呼叫名為 D3D11CreateDevice 的靜態函式來建立裝置和裝置內容。In your Direct3D 11 Universal Windows Platform (UWP) game, you call a static function called D3D11CreateDevice to create the device and the device context.

裝置和裝置內容Devices and device context

一個 Direct3D 11 裝置代表一個虛擬化的圖形卡。A Direct3D 11 device represents a virtualized graphics adapter. 它是用在視訊記憶體中建立資源,例如,將紋理上傳到 GPU、在紋理資源和交換鏈結建立檢視,以及建立紋理取樣器。It's used to create resources in video memory, for example: uploading textures to the GPU, creating views on texture resources and swap chains, and creating texture samplers. 如需 Direct3D 11 裝置介面用途的完整清單,請參閱 ID3D11DeviceID3D11Device1For a complete list of what a Direct3D 11 device interface is used for see ID3D11Device and ID3D11Device1.

Direct3D 11A Direct3D 11. 裝置內容是用於設定管線狀態和產生轉譯命令。device context is used to set pipeline state and generate rendering commands. 例如,Direct3D 11 轉譯鏈結會使用裝置內容來設定轉譯鏈結和繪製場景 (請參閱以下內容)。For example, a Direct3D 11 rendering chain uses a device context to set up the rendering chain and draw the scene (see below). 裝置內容是用於存取 (對應) Direct3D 裝置資源使用的視訊記憶體;也用於更新子資源資料,例如常數緩衝區資料。The device context is used to access (map) video memory used by Direct3D device resources; it's also used to update subresource data, for example constant buffer data. 如需 Direct3D 11 裝置內容用途的完整清單,請參閱 ID3D11DeviceContextID3D11DeviceContext1For a complete list of what a Direct3D 11 device context is used for see ID3D11DeviceContext and ID3D11DeviceContext1. 請注意,我們大部分的範例都使用即時內容直接轉譯到裝置,但是 Direct3D 11 也支援延遲的裝置內容,這主要用於多執行緒處理。Note that most of our samples use an immediate context to render directly to the device, but Direct3D 11 also supports deferred device contexts, which are primarily used for multithreading.

在 Direct3D 11 中,裝置控制代碼和裝置內容控制代碼都是透過呼叫 D3D11CreateDevice 取得。In Direct3D 11, the device handle and device context handle are both obtained by calling D3D11CreateDevice. 您在圖形卡支援的 Direct3D 功能層級,要求一組特定的硬體功能和抓取資訊時,也是使用這個方法。This method is also where you request a specific set of hardware features and retrieve information on Direct3D feature levels supported by the graphics adapter. 如需裝置、裝置內容和執行緒考量的詳細資訊,請參閱 Direct3D 11 中的裝置簡介See Introduction to a Device in Direct3D 11 for more info on devices, device contexts, and threading considerations.

裝置基礎結構、框架緩衝區和轉譯目標檢視Device infrastructure, frame buffers, and render target views

在 Direct3D 11,裝置介面卡和硬體設定是以使用 IDXGIAdapterIDXGIDevice1 COM 介面的 DirectX Graphics Infrastructure (DXGI) API 進行設定。In Direct3D 11, the device adapter and hardware configuration are set with the DirectX Graphics Infrastructure (DXGI) API using the IDXGIAdapter and IDXGIDevice1 COM interfaces. 緩衝區和其他視窗資源 (可見或螢幕上不可見) 都是由特定的 DXGI 介面建立和設定,IDXGIFactory2 Factory 模式實作會取得 DXGI 資源,例如框架緩衝區。Buffers and other window resources (visible or offscreen) are created and configured by specific DXGI interfaces; the IDXGIFactory2 factory pattern implementation acquires DXGI resources such as the frame buffer. 因為 DXGI 擁有交換鏈結,所以 DXGI 介面會用來將畫面呈現在螢幕上,請參閱 IDXGISwapChain1Since DXGI owns the swap chain, a DXGI interface is used to present frames to the screen - see IDXGISwapChain1.

使用 IDXGIFactory2 建立與您的遊戲相容的交換鏈結。Use IDXGIFactory2 to create a swap chain compatible with your game. 您需要建立核心視窗或組合 (XAML 互通性) 的交換鏈結,而不是建立 HWND 的交換鏈結。You need to create a swap chain for a core window, or for composition (XAML interop), instead of creating a swap chain for an HWND.

裝置資源和資源檢視Device resources and resource views

Direct3D 11 在視訊記憶體資源支援一個額外的多型層級稱為檢視。Direct3D 11 supports an additional level of polymorphism on video memory resources known as views. 實際上,您原本有一個 Direct3D 9 紋理物件,而您現在會有兩個物件:一個是保存資料的紋理資源,另一個是指出如何使用檢視進行轉譯的資源檢視。Essentially, where you had a single Direct3D 9 object for a texture, you now have two objects: the texture resource, which holds the data, and the resource view, which indicates how the view is used for rendering. 以資源為基礎的檢視可讓該資源用於特定的目的。A view based on a resource enables that resource to be used for a specific purpose. 例如,2D 紋理資源建立為 ID3D11Texture2D,然後在上面建立著色器資源檢視 (ID3D11ShaderResourceView),便可用來做為著色器中的紋理。For example, a 2D texture resource is created as an ID3D11Texture2D, then a shader resource view (ID3D11ShaderResourceView) is created on it so it can be used as a texture in a shader. 也可以在相同的 2D 紋理資源上建立轉譯目標檢視 (ID3D11RenderTargetView),便可用來做為繪圖表面。A render target view (ID3D11RenderTargetView) can also be created on the same 2D texture resource so that it can be used as a drawing surface. 另一個範例中,在單一紋理資源上使用兩個個別的檢視,以兩種不同的像素格式來表示相同的像素資料。In another example, the same pixel data is represented in 2 different pixel formats by using 2 separate views on a single texture resource.

基礎資源必須以與建立來源檢視類型相容的屬性建立。The underlying resource must be created with properties that are compatible with the type of views that will be created from it. 例如,如果 ID3D11RenderTargetView 套用至介面,則會以 D3D11 系結轉譯 ** _ _ _ 目標** 旗標來建立該介面。For example, if an ID3D11RenderTargetView is applied to a surface, that surface is created with the D3D11_BIND_RENDER_TARGET flag. 介面也必須具有與轉譯相容的 DXGI 介面格式 (請參閱 dxgi _ 格式) 。The surface also has to have a DXGI surface format compatible with rendering (see DXGI_FORMAT).

用於轉譯的大多數資源都是繼承自 ID3D11Resource 介面,而該介面是從 ID3D11DeviceChild 繼承而來。Most of the resources you use for rendering inherit from the ID3D11Resource interface, which inherits from ID3D11DeviceChild. 頂點緩衝區、索引緩衝區、常數緩衝區和著色器都是 Direct3D 11 資源。Vertex buffers, index buffers, constant buffers, and shaders are all Direct3D 11 resources. 輸入配置和取樣器狀態直接繼承自 ID3D11DeviceChildInput layouts and sampler states inherit directly from ID3D11DeviceChild.

資源檢視會使用 DXGI _ 格式列舉值來表示像素格式。Resources views use a DXGI_FORMAT enum value to indicate the pixel format. 並非每個 D3DFMT 都會以 DXGI _ 格式支援。Not every D3DFMT is supported as a DXGI_FORMAT. 例如,在 DXGI 中沒有任何 24bpp RGB 格式相當於 D3DFMT _ R8G8B8。For example, there is no 24bpp RGB format in DXGI that is equivalent to D3DFMT_R8G8B8. 每個 RGB 格式也都不會有 BGR 的對等用法 (DXGI _ 格式 _ R10G10B10A2 _ UNORM 相當於 D3DFMT _ A2B10G10R10,但沒有與 D3DFMT A2R10G10B10) 的直接對等專案 _ 。There are also not BGR equivalents to every RGB format (DXGI_FORMAT_R10G10B10A2_UNORM is equivalent to D3DFMT_A2B10G10R10, but there’s no direct equivalent to D3DFMT_A2R10G10B10). 您應該在建置階段先做規劃,將這些舊版格式的內容轉換為支援的格式。You should plan to convert any content in these legacy formats to supported formats at build-time. 如需 DXGI 格式的完整清單,請參閱 dxgi _ 格式 列舉。For a complete list of DXGI formats see the DXGI_FORMAT enumeration.

Direct3D 裝置資源 (及資源檢視) 會在場景轉譯之前建立。Direct3D device resources (and resource views) are created before the scene is rendered. 裝置內容是用於設定轉譯鏈結,說明如下。Device contexts are used to set up the rendering chain, as explained below.

裝置內容和轉譯鏈結Device context and the rendering chain

在 Direct3D 9 和 Direct3D 10.x,曾有一個管理資源建立、狀態和繪圖的單一 Direct3D 裝置物件。In Direct3D 9 and Direct3D 10.x, there was a single Direct3D device object which managed resource creation, state, and drawing. 在 Direct3D 11,Direct3D 裝置介面仍然會管理資源建立,但所有狀態和繪圖操作都使用 Direct3D 裝置內容來處理。In Direct3D 11, the Direct3D device interface still manages resource creation, but all state and drawing operations are handled by using a Direct3D device context. 以下是如何使用裝置內容 (ID3D11DeviceContext1 介面) 設定轉譯鏈結的範例:Here's an example of how the device context (ID3D11DeviceContext1 interface) is used to set up the rendering chain:

  • 設定和清除轉譯目標檢視 (以及深度樣板檢視)Set and clear render target views (and depth stencil view)
  • 設定輸入組合語言階段 (IA 階段) 的頂點緩衝區、索引緩衝區及輸入配置Set the vertex buffer, index buffer, and input layout for the input assembler stage (IA stage)
  • 將頂點和像素著色器繫結到管線Bind vertex and pixel shaders to the pipeline
  • 將常數緩衝區繫結到著色器Bind constant buffers to shaders
  • 將紋理檢視和取樣器繫結到像素著色器Bind texture views and samplers to the pixel shader
  • 繪製場景Draw the scene

呼叫其中一個 ID3D11DeviceContext::Draw 方法時,會在轉譯目標檢視繪製場景。When one of the ID3D11DeviceContext::Draw methods is called, the scene is drawn on the render target view. 當您完成所有繪圖時,DXGI 介面卡會透過呼叫 IDXGISwapChain1::Present1 用來呈現完整的畫面。When you're done will all your drawing the DXGI adapter is used to present the completed frame by calling IDXGISwapChain1::Present1.

狀態管理State management

具備一組大型個別切換的 Direct3D 9 Managed 狀態設定是以 SetRenderState、SetSamplerState 和 SetTextureStageState 方法進行設定。Direct3D 9 managed state settings with a large set of individual toggles set with the SetRenderState, SetSamplerState, and SetTextureStageState methods. 因為 Direct3D 11 不支援舊版固定函式管線,所以透過撰寫像素著色器 (PS) 來取代 SetTextureStageState。Since Direct3D 11 does not support the legacy fixed-function pipeline, the SetTextureStageState is replaced by writing pixel shaders (PS). Direct3D 9 狀態區塊沒有對應項目。There is no equivalent to a Direct3D 9 state block. Direct3D 11 代之使用 4 種狀態物件來管理狀態,這些物件為轉譯狀態的編組提供更簡單的方式。Direct3D 11 instead manages state through the use of 4 kinds of state objects which provide a more streamlined way to group the rendering state.

例如, _ 您可以使用此和其他相關的狀態設定來建立 DepthStencilState 物件,並在轉譯時使用它來變更狀態,而不是使用 graphicsdevice 搭配 D3DRS ZENABLE。For example, instead of using SetRenderState with D3DRS_ZENABLE, you create a DepthStencilState object with this and other related state settings and use it to change state while rendering.

將 Direct3D 9 應用程式移植到狀態物件時,請注意,您的各種狀態組合會表示為不可變的狀態物件。When porting Direct3D 9 applications to state objects, be aware that your various state combinations are represented as immutable state objects. 它們只應該建立一次,只要有效就可以重複使用。They should be created once and re-used as long as they are valid.

Direct3D 功能層級Direct3D feature levels

Direct3D 有一個用於判斷硬體支援的新機制,稱為「功能層級」。Direct3D has a new mechanism for determining hardware support called feature levels. 功能層級透過允許您要求一組定義完整的 GPU 功能,簡化了判斷圖形卡作用的工作。Feature levels simplify the task of figuring out what the graphics adapter can do by allowing you to request a well-defined set of GPU functionality. 例如,9 1 的 _ 功能層級會執行 Direct3D 9 圖形介面卡所提供的功能,包括著色器型號2.x。For example, the 9_1 feature level implements the functionality provided by Direct3D 9 graphics adapters, including shader model 2.x. 由於 9 _ 1 是最低的功能層級,因此您可以預期所有裝置都支援頂點著色器和圖元著色器,這些都是 Direct3D 9 可程式化著色器模型所支援的相同階段。Since 9_1 is the lowest feature level, you can expect all devices to support a vertex shader and a pixel shader, which were the same stages supported by the Direct3D 9 programmable shader model.

您的遊戲將使用 D3D11CreateDevice 建立 Direct3D 裝置和裝置內容。Your game will use D3D11CreateDevice to create the Direct3D device and device context. 當您呼叫這個函式時,要提供您的遊戲可支援的功能層級清單,When you call this function you provide a list of feature levels that your game can support. 它會傳回該清單中可支援的最高功能層級。It will return the highest supported feature level from that list. 例如,如果您的遊戲可以使用 BC4/BC5 材質 (DirectX 10 硬體) 的功能,您會 _ _ 在支援的功能層級清單中包含至少 9 1 和 10 0。For example if your game can use BC4/BC5 textures (a feature of DirectX 10 hardware), you would include at least 9_1 and 10_0 in the list of supported feature levels. 如果遊戲是在 DirectX 9 硬體上執行,而且無法使用 BC4/BC5 紋理,則 D3D11CreateDevice 會傳回 9 _ 1。If the game is running on DirectX 9 hardware and BC4/BC5 textures can't be used, then D3D11CreateDevice will return 9_1. 然後,您的遊戲可以切換回不同的紋理格式 (和較小的紋理)。Then your game can fall back to a different texture format (and smaller textures).

如果您決定擴充 Direct3D 9 遊戲以支援更高的 Direct3D 功能層級,最好先完成現有 Direct3D 9 圖形程式碼的移植。If you decide to extend your Direct3D 9 game to support higher Direct3D feature levels then it's better to finish porting your existing Direct3D 9 graphics code first. 等到您的遊戲可以在 Direct3D 11 運作之後,比較容易以增強的圖形新增其他轉譯路徑。After you have your game working in Direct3D 11, it's easier to add additional rendering paths with enhanced graphics.

如需功能層級支援的詳細說明,請參閱 Direct3D 功能層級See Direct3D feature levels for a detailed explanation of feature level support. 如需 Direct3D 11 功能的完整清單,請參閱 Direct3D 11 功能Direct3D 11.1 功能See Direct3D 11 Features and Direct3D 11.1 Features for a full list of Direct3D 11 features.

功能層級和可程式化的管線Feature levels and the programmable pipeline

Direct3D 9 推出之後,硬體仍持續不斷地演進,而可程式化的圖形管線也增加了多個新的選擇性階段。Hardware has continue to evolve since Direct3D 9, and several new optional stages have been added to the programmable graphics pipeline. 您所擁有的圖形管線選項組,隨 Direct3D 功能層級不同而異。The set of options you have for the graphics pipeline varies with the Direct3D feature level. 功能層級 10.0 包含幾何著色器階段,以及在 GPU 上物件多重轉譯的選擇性資料流輸出。Feature level 10.0 includes the geometry shader stage with optional stream out for multipass rendering on the GPU. 功能等級 11 _ 0 包含用來進行硬體鑲嵌的輪廓著色器和網域著色器。Feature level 11_0 include the hull shader and domain shader for use with hardware tessellation. 功能等級 11 _ 0 也包含 DirectCompute 著色器的完整支援,而功能層級2.x 則只包含有限形式的 DirectCompute 支援。Feature level 11_0 also includes full support for DirectCompute shaders, while feature levels 10.x only include support for a limited form of DirectCompute.

所有著色器都是使用對應到 Direct3D 功能層級的著色器設定檔,以 HLSL 撰寫而成。All shaders are written in HLSL using a shader profile that corresponds to a Direct3D feature level. 著色器設定檔的相容性,因此使用 vs _ 4 _ 0 _ 層級 _ 9 _ 1 或 ps 4 0 層級 9 1 編譯的 HLSL 著色器 _ _ _ _ _ 將可在所有裝置上運作。Shader profiles are upwards compatible, so an HLSL shader that compiles using vs_4_0_level_9_1 or ps_4_0_level_9_1 will work across all devices. 著色器設定檔與舊版不相容,因此使用 vs 4 1 編譯的著色器 _ _ 只會在功能層級 10 _ 1、11 _ 0 或 11 1 的裝置上運作 _ 。Shader profiles are not downlevel compatible, so a shader compiled using vs_4_1 will only work on feature level 10_1, 11_0, or 11_1 devices.

著色器的 Direct3D 9 管理常數使用共用陣列搭配 SetVertexShaderConstant 和 SetPixelShaderConstant。Direct3D 9 managed constants for shaders using a shared array with SetVertexShaderConstant and SetPixelShaderConstant. Direct3D 11 則使用常數緩衝區,這些是像頂點緩衝區或索引緩衝區等的資源。Direct3D 11 uses constant buffers, which are resources like a vertex buffer or index buffer. 常數緩衝區的設計目的是要有效率地更新。Constant buffers are designed to be updated efficiently. 這個做法不是將所有著色器常數組織成單一全域陣列,而是將您的常數組織為邏輯群組,並透過一或多個常數緩衝區加以管理。Instead of having all the shader contants organized into a single global array you organize your constants into logical groupings and manage them through one or more constant buffers. 當您將 Direct3D 9 遊戲移植到 Direct3D 11 時,需要先規劃如何組織您的常數緩衝區,才能適當地更新常數。When you port your Direct3D 9 game to Direct3D 11, plan to organize your constant buffers so that you can update them appropriately. 例如,將每個畫面未更新的著色器常數編組成個別常數緩衝區,所以您不必經常將該資料以及更多動態著色器常數上傳到圖形卡。For example, group shader constants that aren't updated every frame into a separate constant buffer, so that you don't have to constantly upload that data to the graphics adapter along with your more dynamic shader constants.

注意   大部分的 Direct3D 9 應用程式都廣泛使用著色器,但偶爾會混合使用舊版的固定函式行為。Note   Most Direct3D 9 applications made extensive use of shaders, but occasionally mixed in use of the legacy fixed-function behavior. 請注意,Direct3D 11 只使用可程式化的著色模型。Note that Direct3D 11 only uses a programmable shading model. Direct3D 9 的舊版固定函式功能已過時。The legacy fixed-function features of Direct3D 9 are deprecated.