ID3D11DeviceContext
Overview
The ID3D11DeviceContext interface represents a device context, which is used to issue rendering commands to the GPU. It provides methods for drawing, state management, resource updates, and query handling.
Syntax
interface ID3D11DeviceContext : ID3D11DeviceChild {
void VSSetConstantBuffers(UINT StartSlot, UINT NumBuffers, ID3D11Buffer *const *ppConstantBuffers);
void PSSetShaderResources(UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView *const *ppShaderResourceViews);
// ... many more methods ...
}
Methods
| Method | Description |
|---|---|
Draw | Renders non-indexed, non-instanced primitives. |
DrawIndexed | Renders indexed primitives. |
DrawInstanced | Renders instanced primitives. |
DrawIndexedInstanced | Renders indexed, instanced primitives. |
| Method | Description |
|---|---|
VSSetShader | Sets the vertex shader to the device context. |
PSSetShader | Sets the pixel shader to the device context. |
RSSetViewports | Sets the viewports for rasterizer stage. |
OMSetRenderTargets | Sets render targets and depth-stencil view. |
| Method | Description |
|---|---|
UpdateSubresource | Copies CPU data to a subresource of a GPU resource. |
CopyResource | Copies an entire resource. |
CopySubresourceRegion | Copies a region of one resource to another. |
Properties
Device– Retrieves the parentID3D11Deviceobject.FeatureLevel– Indicates the highest feature level supported.
Remarks
Device contexts are divided into immediate and deferred contexts. The immediate context executes commands as they are issued, while deferred contexts record command lists for later execution on the immediate context.
Example
// Create device and immediate context
ID3D11Device* gDevice = nullptr;
ID3D11DeviceContext* gContext = nullptr;
D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
nullptr,
0,
D3D11_SDK_VERSION,
&gDevice,
nullptr,
&gContext);
// Clear render target
float clearColor[4] = {0.1f, 0.2f, 0.3f, 1.0f};
ID3D11RenderTargetView* rtv = nullptr; // Assume created elsewhere
gContext->ClearRenderTargetView(rtv, clearColor);
// Draw a triangle
gContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
gContext->Draw(3, 0);