Introduction to a Device in Direct3D 11
The Direct3D 11 object model separates resource creation and rendering functionality into a device and one or more contexts; this separation is designed to facilitate multithreading.
Direct3D11对象模型将资源创建和渲染功能分离为设备和一个或多个上下文;这种分离是为了方便多线程处理而设计的。
Device
A device is used to create resources and to enumerate the capabilities of a display adapter. In Direct3D 11, a device is represented with an ID3D11Device interface.
Each application must have at least one device, most applications only create one device. Create a device for one of the hardware drivers installed on your machine by calling D3D11CreateDevice or D3D11CreateDeviceAndSwapChain and specify the driver type with the D3D_DRIVER_TYPE flag. Each device can use one or more device contexts, depending on the functionality desired.
设备用于创建资源并枚举显示适配器的功能。在Direct3D11中,设备用ID3D11Device接口表示。
Device Context
A device context contains the circumstance or setting in which a device is used. More specifically, a device context is used to set pipeline state and generate rendering commands using the resources owned by a device. Direct3D 11 implements two types of device contexts, one for immediate rendering and the other for deferred rendering; both contexts are represented with an ID3D11DeviceContext interface.
Immediate Context
An immediate context renders directly to the driver. Each device has one and only one immediate context which can retrieve data from the GPU. An immediate context can be used to immediately render (or play back) a command list.
There are two ways to get an immediate context:
By calling either D3D11CreateDevice or D3D11CreateDeviceAndSwapChain.
By calling ID3D11Device::GetImmediateContext.
即时上下文
即时上下文直接呈现给驱动程序的即时上下文。每个设备都有一个并且只有一个即时上下文,可以从GPU中检索数据。即时上下文可用于立即渲染(或回放)命令列表。
有两种方法可以获得即时上下文:
通过调用D3D11CreateDevice或D3D11CCreateDeviceAndSwapChain。
通过调用ID3D11Device::GetImmediateContext。
Deferred Context
A deferred context records GPU commands into a command list. A deferred context is primarily used for multithreading and is not necessary for a single-threaded application. A deferred context is generally used by a worker thread instead of the main rendering thread. When you create a deferred context, it does not inherit any state from the immediate context.
To get a deferred context, call ID3D11Device::CreateDeferredContext.
Any context -- immediate or deferred -- can be used on any thread as long as the context is only used in one thread at a time.
延迟上下文
延迟上下文将GPU命令记录到命令列表中。延迟上下文主要用于多线程,而不是单线程应用程序所必需的。延迟上下文通常由工作线程而不是主呈现线程使用。创建延迟上下文时,它不会从直接上下文继承任何状态。
若要获取延迟上下文,请调用ID3D11Device::CreateDeferredContext。
任何上下文(即时或延迟)都可以在任何线程上使用,只要上下文一次只在一个线程中使用即可。
Threading Considerations
This table highlights the differences in the threading model in Direct3D 11 from prior versions of Direct3D.
Differences between Direct3D 11 and previous versions of Direct3D:
All ID3D11Device interface methods are free-threaded, which means it is safe to have multiple threads call the functions at the same time.
All ID3D11DeviceChild-derived interfaces (ID3D11Buffer, ID3D11Query, etc.) are free-threaded.
Direct3D 11 splits resource creating and rendering into two interfaces. Map, Unmap, Begin, End, and GetData are implemented on ID3D11DeviceContext because ID3D11Device strongly defines the order of operations. ID3D11Resource and ID3D11Asynchronous interfaces also implement methods for free-threaded operations.
The ID3D11DeviceContext methods (except for those that exist on ID3D11DeviceChild) are not free-threaded, that is, they require single threading. Only one thread may safely be calling any of its methods (Draw, Copy, Map, etc.) at a time.
In general, free-threading minimizes the number of synchronization primitives used as well as their duration. However, an application that uses synchronization held for a long time can directly impact how much concurrency an application can expect to achieve.
线程注意事项
此表突出显示了Direct3D 11中线程模型与早期版本的Direct3D之间的差异。
Direct3D 11和以前版本的Direct3D之间的差异:
所有ID3D11Device接口方法都是自由线程的,这意味着多个线程同时调用函数是安全的。
所有ID3D11DeviceChild派生接口(ID3D11Buffer、ID3D11Query等)都是自由线程的。
Direct3D11将资源创建和渲染划分为两个接口。Map、Unmap、Begin、End和GetData在ID3D11DeviceContext上实现,因为ID3D11Device强烈定义了操作顺序。ID3D11Resource和ID3D11Asynchronous接口还实现了用于自由线程操作的方法。
ID3D11DeviceContext方法(ID3D11Devices Child上存在的方法除外)不是自由线程的,也就是说,它们需要单线程。一次只有一个线程可以安全地调用它的任何方法(Draw、Copy、Map等)。
通常,自由线程可以最大限度地减少所使用的同步原语的数量及其持续时间。然而,使用长时间保持同步的应用程序可能会直接影响应用程序预期实现的并发量。