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對象模型將資源創(chuàng)建和渲染功能分離為設(shè)備和一個或多個上下文;這種分離是為了方便多線程處理而設(shè)計的。
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.
設(shè)備用于創(chuàng)建資源并枚舉顯示適配器的功能。在Direct3D11中,設(shè)備用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.
即時上下文
即時上下文直接呈現(xiàn)給驅(qū)動程序的即時上下文。每個設(shè)備都有一個并且只有一個即時上下文,可以從GPU中檢索數(shù)據(jù)。即時上下文可用于立即渲染(或回放)命令列表。
有兩種方法可以獲得即時上下文:
通過調(diào)用D3D11CreateDevice或D3D11CCreateDeviceAndSwapChain。
通過調(diào)用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命令記錄到命令列表中。延遲上下文主要用于多線程,而不是單線程應(yīng)用程序所必需的。延遲上下文通常由工作線程而不是主呈現(xiàn)線程使用。創(chuàng)建延遲上下文時,它不會從直接上下文繼承任何狀態(tài)。
若要獲取延遲上下文,請調(diào)用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接口方法都是自由線程的,這意味著多個線程同時調(diào)用函數(shù)是安全的。
所有ID3D11DeviceChild派生接口(ID3D11Buffer、ID3D11Query等)都是自由線程的。
Direct3D11將資源創(chuàng)建和渲染劃分為兩個接口。Map、Unmap、Begin、End和GetData在ID3D11DeviceContext上實(shí)現(xiàn),因?yàn)镮D3D11Device強(qiáng)烈定義了操作順序。ID3D11Resource和ID3D11Asynchronous接口還實(shí)現(xiàn)了用于自由線程操作的方法。
ID3D11DeviceContext方法(ID3D11Devices Child上存在的方法除外)不是自由線程的,也就是說,它們需要單線程。一次只有一個線程可以安全地調(diào)用它的任何方法(Draw、Copy、Map等)。
通常,自由線程可以最大限度地減少所使用的同步原語的數(shù)量及其持續(xù)時間。然而,使用長時間保持同步的應(yīng)用程序可能會直接影響應(yīng)用程序預(yù)期實(shí)現(xiàn)的并發(fā)量。