Diligent Engine API Reference: Diligent::IDeviceContext Class Reference

Diligent Engine API Reference

Public Member Functions | List of all members
Diligent::IDeviceContext Class Referenceabstract

Device context interface. More...

Inheritance diagram for Diligent::IDeviceContext:

## Public Member Functions
virtual void | QueryInterface (const Diligent::INTERFACE_ID &IID, IObject **ppInterface)=0
| Queries the specific interface, see IObject::QueryInterface() for details.
virtual void | SetPipelineState (IPipelineState *pPipelineState)=0
| Sets the pipeline state. More...
virtual void | TransitionShaderResources (IPipelineState *pPipelineState, IShaderResourceBinding *pShaderResourceBinding)=0
| Transitions shader resources to the require states. More...
virtual void | CommitShaderResources (IShaderResourceBinding *pShaderResourceBinding, Uint32 Flags)=0
| Commits shader resources to the device context. More...
virtual void | SetStencilRef (Uint32 StencilRef)=0
| Sets the stencil reference value. More...
virtual void | SetBlendFactors (const float *pBlendFactors=nullptr)=0
virtual void | SetVertexBuffers (Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags)=0
| Binds vertex buffers to the pipeline. More...
virtual void | InvalidateState ()=0
| Invalidates the cached context state. More...
virtual void | SetIndexBuffer (IBuffer *pIndexBuffer, Uint32 ByteOffset)=0
| Binds an index buffer to the pipeline. More...
virtual void | SetViewports (Uint32 NumViewports, const Viewport *pViewports, Uint32 RTWidth, Uint32 RTHeight)=0
| Sets an array of viewports. More...
virtual void | SetScissorRects (Uint32 NumRects, const Rect *pRects, Uint32 RTWidth, Uint32 RTHeight)=0
| Sets active scissor rects. More...
virtual void | SetRenderTargets (Uint32 NumRenderTargets, ITextureView *ppRenderTargets[], ITextureView *pDepthStencil)=0
| Binds one or more render targets and the depth-stencil buffer to the pipeline. It also sets the viewport to match the first non-null render target or depth-stencil buffer. More...
virtual void | Draw (DrawAttribs &DrawAttribs)=0
| Executes a draw command. More...
virtual void | DispatchCompute (const DispatchComputeAttribs &DispatchAttrs)=0
| Executes a dispatch compute command. More...
virtual void | ClearDepthStencil (ITextureView *pView, Uint32 ClearFlags=CLEAR_DEPTH_FLAG, float fDepth=1.f, Uint8 Stencil=0)=0
| Clears a depth-stencil view. More...
virtual void | ClearRenderTarget (ITextureView *pView, const float *RGBA=nullptr)=0
| Clears a render target view. More...
virtual void | FinishCommandList (ICommandList **ppCommandList)=0
| Finishes recording commands and generates a command list. More...
virtual void | ExecuteCommandList (ICommandList *pCommandList)=0
| Executes recorded commands in a command list. More...
virtual void | Flush ()=0
| Flushes the command buffer.
virtual void | SetSwapChain (ISwapChain *pSwapChain)=0
| Sets the swap chain in the device context. More...

## Detailed Description

Device context interface.

Remarks
  Device context keeps strong references to all objects currently bound to the pipeline: buffers, states, samplers, shaders, etc. The context also keeps strong reference to the device and the swap chain.

## Member Function Documentation

## ◆ ClearDepthStencil()

virtual void Diligent::IDeviceContext::ClearDepthStencil | ( | ITextureView * | pView,
|  | Uint32 | ClearFlags = `CLEAR_DEPTH_FLAG`,
|  | float | fDepth = `1.f`,
|  | Uint8 | Stencil = `0`
| ) |  |
| pure virtual

Clears a depth-stencil view.

Parameters

  [in] | pView | - Pointer to ITextureView interface to clear. The view type must be Diligent::TEXTURE_VIEW_DEPTH_STENCIL.
  [in] | ClearFlags | - Idicates which parts of the buffer to clear, see Diligent::CLEAR_DEPTH_STENCIL_FLAGS.
  [in] | fDepth | - Value to clear depth part of the view with.
  [in] | Stencil | - Value to clear stencil part of the view with.

Remarks
  The full extent of the view is always cleared. Viewport and scissor settings are not applied.

Note
  The depth-stencil view must be bound to the pipeline for clear operation to be performed.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ ClearRenderTarget()

virtual void Diligent::IDeviceContext::ClearRenderTarget | ( | ITextureView * | pView,
|  | const float * | RGBA = `nullptr`
| ) |  |
| pure virtual

Clears a render target view.

Parameters

  [in] | pView | - Pointer to ITextureView interface to clear. The view type must be Diligent::TEXTURE_VIEW_RENDER_TARGET.
  [in] | RGBA | - A 4-component array that represents the color to fill the render target with. If nullptr is provided, the default array {0,0,0,0} will be used.

Remarks
  The full extent of the view is always cleared. Viewport and scissor settings are not applied.

Note
  The render target view must be bound to the pipeline for clear operation to be performed.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ CommitShaderResources()

virtual void Diligent::IDeviceContext::CommitShaderResources | ( | IShaderResourceBinding * | pShaderResourceBinding,
|  | Uint32 | Flags
| ) |  |
| pure virtual

Commits shader resources to the device context.

Parameters

  [in] | pShaderResourceBinding | - Shader resource binding whose resources will be committed. If pipeline state contains no shader resources, this parameter can be null.
  [in] | Flags | - Additional flags for the operation. See Diligent::COMMIT_SHADER_RESOURCES_FLAG for a list of allowed values.

Remarks
  Pipeline state object that was used to create the shader resource binding must be bound to the pipeline when CommitShaderResources() is called. If no pipeline state object is bound or the pipeline state object does not match shader resource binding, the method will fail.
  If Diligent::COMMIT_SHADER_RESOURCES_FLAG_TRANSITION_RESOURCES flag is specified, the engine will also transition all shader resources to the correct state. If the flag is not specified, it is assumed that all resources are already in correct states.
  Resources can be explicitly transitioned to the required states by calling IDeviceContext::TransitionShaderResources()

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ DispatchCompute()

virtual void Diligent::IDeviceContext::DispatchCompute | ( | const DispatchComputeAttribs & | DispatchAttrs | ) |
| pure virtual

Executes a dispatch compute command.

Parameters

  [in] | DispatchAttrs | - Structure describing dispatch command attributes, see DispatchComputeAttribs for details.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ Draw()

virtual void Diligent::IDeviceContext::Draw | ( | DrawAttribs & | DrawAttribs | ) |
| pure virtual

Executes a draw command.

Parameters

  [in] | DrawAttribs | - Structure describing draw command attributes, see DrawAttribs for details.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ ExecuteCommandList()

virtual void Diligent::IDeviceContext::ExecuteCommandList | ( | ICommandList * | pCommandList | ) |
| pure virtual

Executes recorded commands in a command list.

Parameters

  [in] | pCommandList | - Pointer to the command list to executre.

Remarks
  After command list is executed, it is no longer valid and should be released.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ FinishCommandList()

virtual void Diligent::IDeviceContext::FinishCommandList | ( | ICommandList ** | ppCommandList | ) |
| pure virtual

Finishes recording commands and generates a command list.

Parameters

  [out] | ppCommandList | - Memory location where pointer to the recorded command list will be written.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ InvalidateState()

virtual void Diligent::IDeviceContext::InvalidateState | ( |  | ) |
| pure virtual

Invalidates the cached context state.

This method should be called by say Unity plugin before (or after) issuing draw commands to invalidate cached states

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ SetBlendFactors()

virtual void Diligent::IDeviceContext::SetBlendFactors | ( | const float * | pBlendFactors = `nullptr` | ) |
| pure virtual

Parameters

  [in] | pBlendFactors | - Array of four blend factors, one for each RGBA component. Theses factors are used if the blend state uses one of the Diligent::BLEND_FACTOR_BLEND_FACTOR or Diligent::BLEND_FACTOR_INV_BLEND_FACTOR blend factors. If nullptr is provided, default blend factors array {1,1,1,1} will be used.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ SetIndexBuffer()

virtual void Diligent::IDeviceContext::SetIndexBuffer | ( | IBuffer * | pIndexBuffer,
|  | Uint32 | ByteOffset
| ) |  |
| pure virtual

Binds an index buffer to the pipeline.

Parameters

  [in] | pIndexBuffer | - Pointer to the index buffer. The buffer must have been created with the Diligent::BIND_INDEX_BUFFER flag.
  [in] | ByteOffset | - Offset from the beginning of the buffer to the start of index data.

Remarks
  The device context keeps strong reference to the index buffer. Thus an index buffer object cannot be released until it is unbound from the context.

Implemented in Diligent::DeviceContextBase< IDeviceContextD3D12 >, Diligent::DeviceContextBase< IDeviceContextGL >, Diligent::DeviceContextBase< IDeviceContextD3D11 >, Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ SetPipelineState()

virtual void Diligent::IDeviceContext::SetPipelineState | ( | IPipelineState * | pPipelineState | ) |
| pure virtual

Sets the pipeline state.

Parameters

  [in] | pPipelineState | - Pointer to IPipelineState interface to bind to the context.

Implemented in Diligent::DeviceContextBase< IDeviceContextD3D12 >, Diligent::DeviceContextBase< IDeviceContextGL >, Diligent::DeviceContextBase< IDeviceContextD3D11 >, Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ SetRenderTargets()

virtual void Diligent::IDeviceContext::SetRenderTargets | ( | Uint32 | NumRenderTargets,
|  | ITextureView * | ppRenderTargets[],
|  | ITextureView * | pDepthStencil
| ) |  |
| pure virtual

Binds one or more render targets and the depth-stencil buffer to the pipeline. It also sets the viewport to match the first non-null render target or depth-stencil buffer.

Parameters

  [in] | NumRenderTargets | - Number of render targets to bind.
  [in] | ppRenderTargets | - Array of pointers to ITextureView that represent the render targets to bind to the device. The type of each view in the array must be Diligent::TEXTURE_VIEW_RENDER_TARGET.
  [in] | pDepthStencil | - Pointer to the ITextureView that represents the depth stencil to bind to the device. The view type must be Diligent::TEXTURE_VIEW_DEPTH_STENCIL.

Remarks
  The device context will keep strong references to all bound render target and depth-stencil views. Thus these views (and consequently referenced textures) cannot be released until they are unbound from the context.
  Any render targets not defined by this call are set to nullptr.
  You can set the default render target and depth stencil using the following call:

      pContext->SetRenderTargets(0, nullptr, nullptr);

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ SetScissorRects()

virtual void Diligent::IDeviceContext::SetScissorRects | ( | Uint32 | NumRects,
|  | const Rect * | pRects,
|  | Uint32 | RTWidth,
|  | Uint32 | RTHeight
| ) |  |
| pure virtual

Sets active scissor rects.

Parameters

  [in] | NumRects | - Number of scissor rectangles to set.
  [in] | pRects | - An array of Rect structures describing the scissor rectangles to bind.
  [in] | RTWidth | - Render target width. If 0 is provided, width of the currently bound render target will be used.
  [in] | RTHeight | - Render target height. If 0 is provided, height of the currently bound render target will be used.

Remarks
  DirectX and OpenGL use different window coordinate systems. In DirectX, the coordinate system origin is in the left top corner of the screen with Y axis pointing down. In OpenGL, the origin is in the left bottom corener of the screen with Y axis pointing up. Render target size is required to convert viewport from DirectX to OpenGL coordinate system if OpenGL device is used.
  All scissor rects must be set atomically as one operation. Any rects not defined by the call are disabled.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ SetStencilRef()

virtual void Diligent::IDeviceContext::SetStencilRef | ( | Uint32 | StencilRef | ) |
| pure virtual

Sets the stencil reference value.

Parameters

  [in] | StencilRef | - Stencil reference value.

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ SetSwapChain()

virtual void Diligent::IDeviceContext::SetSwapChain | ( | ISwapChain * | pSwapChain | ) |
| pure virtual

Sets the swap chain in the device context.

The swap chain is used by the device context to work with the default framebuffer. Specifically, if the swap chain is set in the context, the following commands can be used:

- SetRenderTargets(0, nullptr, nullptr) - to bind the default back buffer & depth buffer
- SetViewports(1, nullptr, 0, 0) - to set the viewport to match the size of the back buffer
- ClearRenderTarget(nullptr, color) - to clear the default back buffer
- ClearDepthStencil(nullptr, ...) - to clear the default depth buffer The swap chain is automatically initialized for immediate and all deferred contexts by factory functions EngineFactoryD3D11Impl::CreateSwapChainD3D11(), EngineFactoryD3D12Impl::CreateSwapChainD3D12(), and EngineFactoryOpenGLImpl::CreateDeviceAndSwapChainGL(). However, when the engine is initialized by attaching to existing d3d11/d3d12 device or OpenGL/GLES context, the swap chain needs to be set manually if the device context will be using any of the commands above.
  Device context keeps strong reference to the swap chain.

Implemented in Diligent::DeviceContextBase< IDeviceContextD3D12 >, Diligent::DeviceContextBase< IDeviceContextGL >, and Diligent::DeviceContextBase< IDeviceContextD3D11 >.

## ◆ SetVertexBuffers()

virtual void Diligent::IDeviceContext::SetVertexBuffers | ( | Uint32 | StartSlot,
|  | Uint32 | NumBuffersSet,
|  | IBuffer ** | ppBuffers,
|  | Uint32 * | pStrides,
|  | Uint32 * | pOffsets,
|  | Uint32 | Flags
| ) |  |
| pure virtual

Binds vertex buffers to the pipeline.

Parameters

  [in] | StartSlot | - The first input slot for binding. The first vertex buffer is explicitly bound to the start slot; each additional vertex buffer in the array is implicitly bound to each subsequent input slot.
  [in] | NumBuffersSet | - The number of vertex buffers in the array.
  [in] | ppBuffers | - A pointer to an array of vertex buffers.
  [in] | pStrides | - Pointer to an array of stride values; one stride value for each buffer in the vertex-buffer array. Each stride is the size (in bytes) of the elements that are to be used from that vertex buffer. If this parameter is nullptr, tight strides from the input layout will be used for each buffer. See IPipelineState::GetTightStrides().
  [in] | pOffsets | - Pointer to an array of offset values; one offset value for each buffer in the vertex-buffer array. Each offset is the number of bytes between the first element of a vertex buffer and the first element that will be used. If this parameter is nullptr, zero offsets for all buffers will be used.
  [in] | Flags | - Additional flags for the operation. See Diligent::SET_VERTEX_BUFFERS_FLAGS for a list of allowed values.

Remarks
  The device context keeps strong references to all bound vertex buffers. Thus a buffer cannot be released until it is unbound from the context.
  It is suggested to specify Diligent::SET_VERTEX_BUFFERS_FLAG_RESET flag whenever possible. This will assure that no buffers from previous draw calls are are bound to the pipeline.

Implemented in Diligent::DeviceContextBase< IDeviceContextD3D12 >, Diligent::DeviceContextBase< IDeviceContextGL >, Diligent::DeviceContextBase< IDeviceContextD3D11 >, Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ SetViewports()

virtual void Diligent::IDeviceContext::SetViewports | ( | Uint32 | NumViewports,
|  | const Viewport * | pViewports,
|  | Uint32 | RTWidth,
|  | Uint32 | RTHeight
| ) |  |
| pure virtual

Sets an array of viewports.

Parameters

  [in] | NumViewports | - Number of viewports to set.
  [in] | pViewports | - An array of Viewport structures describing the viewports to bind.
  [in] | RTWidth | - Render target width. If 0 is provided, width of the currently bound render target will be used.
  [in] | RTHeight- | Render target height. If 0 is provided, height of the currently bound render target will be used.

Remarks
  DirectX and OpenGL use different window coordinate systems. In DirectX, the coordinate system origin is in the left top corner of the screen with Y axis pointing down. In OpenGL, the origin is in the left bottom corener of the screen with Y axis pointing up. Render target size is required to convert viewport from DirectX to OpenGL coordinate system if OpenGL device is used.
  All viewports must be set atomically as one operation. Any viewports not defined by the call are disabled.
  You can set the viewport size to match the currently bound render target using the following call:

      pContext->SetViewports(1, nullptr, 0, 0);

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.

## ◆ TransitionShaderResources()

virtual void Diligent::IDeviceContext::TransitionShaderResources | ( | IPipelineState * | pPipelineState,
|  | IShaderResourceBinding * | pShaderResourceBinding
| ) |  |
| pure virtual

Transitions shader resources to the require states.

Parameters

  [in] | pPipelineState | - Pipeline state object that was used to create the shader resource binding.
  [in] | pShaderResourceBinding | - Shader resource binding whose resources will be transitioned.

Remarks
  This method explicitly transitiones all resources to the correct states. If this method was called, there is no need to specify Diligent::COMMIT_SHADER_RESOURCES_FLAG_TRANSITION_RESOURCES when calling IDeviceContext::CommitShaderResources()

Implemented in Diligent::DeviceContextD3D12Impl, Diligent::DeviceContextD3D11Impl, and Diligent::DeviceContextGLImpl.