从 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. 自 Direct3D 9 起,图形 API 的布局已更改,设备上下文的概念已经扩展,并且已添加一个 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 图形基础结构 (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 出厂模式实现获取帧缓冲区之类的 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. 在另一个示例中,通过对一个纹理资源使用 2 个不同的视图以 2 种不同的像素格式表示相同的像素数据。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 接口,而此接口又继承自 ID3D11DeviceChildMost 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 中没有与 D3DFMT R8G8B8 等效的 24bpp RGB 格式 _ 。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 通过使用 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 不支持传统的固定函数管道,因此 SetTextureStageState 替换为写入像素着色器 (PS)。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 对象,而不是将 SetRenderState 与 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 着色器的完全支持,而功能级别4.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 _ level _ 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.