Direct3D11 Interoperability

This page describes interfaces and methods that provide interoperability with Direct3D11.

Accessing Native D3D11 objects

  • ID3D11Buffer *IBufferD3D11::GetD3D11Buffer() – returns a pointer to the ID3D11Buffer interface of the internal Direct3D11 buffer object.
  • ID3D11Resource* ITextureD3D11::GetD3D11Texture()  – returns a pointer to the ID3D11Resource interface of the internal Direct3D11 texture object. Depending on the texture type, the pointer can be cast to:
  • ID3D11View* IBufferViewD3D11()::GetD3D11View() – returns a pointer to the ID3D11View interface of the internal d3d11 object representing the buffer view. Depending on the view type, the pointer can be cast to
  • ID3D11View* ITextureViewD3D11::GetD3D11View() – returns a pointer to the ID3D11View interface of the internal d3d11 object representing the texture view. Depending on the view type, the pointer can be cast to
  • ID3D11Device* IRenderDeviceD3D11::GetD3D11Device() – returns a pointer to the native D3D11 device object
  • ID3D11DeviceContext* IDeviceContextD3D11::GetD3D11DeviceContext() – returns a pointer to the native ID3D11DeviceContext object
  • ID3D11SamplerState* ISamplerD3D11::GetD3D11SamplerState()  – returns a pointer to the native ID3D11SamplerState interface.
  • ID3D11DeviceChild* IShaderD3D11::GetD3D11Shader() – returns a pointer to the ID3D11DeviceChild interface of the D3D11 shader object. Depending on the shader type, the pointer can be cast to
  • ID3D11BlendState* IPipelineStateD3D11::GetD3D11BlendState() – returns a pointer to the ID3D11BlendState interface of the internal Direct3D11 object
  • ID3D11RasterizerState* IPipelineStateD3D11::GetD3D11RasterizerState() – returns a pointer to the ID3D11RasterizerState interface of the internal Direct3D11 object
  • ID3D11DepthStencilState* IPipelineStateD3D11::GetD3D11DepthStencilState()  – returns a pointer to the ID3D11DepthStencilState interface of the internal Direct3D11 object
  • ID3D11InputLayout* IPipelineStateD3D11::GetD3D11InputLayout()  – returns a pointer to the ID3D11InputLayout interface of the internal Direct3D11 object
  • ID3D11VertexShader* IPipelineStateD3D11::GetD3D11VertexShader() – returns a pointer to the ID3D11VertexShader interface of the internal vertex shader object
  • ID3D11PixelShader* IPipelineStateD3D11::GetD3D11PixelShader()  – returns a pointer to the ID3D11PixelShader interface of the internal pixel shader object
  • ID3D11GeometryShader* IPipelineStateD3D11::GetD3D11GeometryShader() – returns a pointer to the ID3D11GeometryShader interface of the internal geometry shader object
  • ID3D11DomainShader* IPipelineStateD3D11::GetD3D11DomainShader() – returns a pointer to the ID3D11DomainShader interface of the internal domain shader object
  • ID3D11HullShader* IPipelineStateD3D11::GetD3D11HullShader() – returns a pointer to the ID3D11HullShader interface of the internal hull shader object
  • ID3D11ComputeShader* IPipelineStateD3D11::GetD3D11ComputeShader() – returns a pointer to the ID3D11ComputeShader interface of the internal compute shader object

Note that the methods listed above do not call AddRef() on the returned interface, so the application must not call Release().

Examples:

 Creating Diligent Engine Objects from D3D11 Resources

  • void IRenderDeviceD3D11::CreateBufferFromD3DResource(ID3D11Buffer *pd3d11Buffer, const BufferDesc& BuffDesc, IBuffer **ppBuffer)  – creates a diligent engine buffer object from the native d3d11 buffer. The method takes a pointer to the native d3d11 buffer pd3d11Buffer, buffer description BuffDesc and writes a pointer to the IBuffer interface at the memory location pointed to by ppBuffer. Most members of the  BuffDesc structure can keep default values, and the engine will populate them based on the attributes of D3D11 buffer. The only field that must be populated is BufferDesc::Format, when creating a formatted buffer.
  • void IRenderDeviceD3D11::CreateTextureFromD3DResource(ID3D11Texture1D *pd3d11Texture, ITexture **ppTexture)
    void IRenderDeviceD3D11::CreateTextureFromD3DResource(ID3D11Texture2D *pd3d11Texture, ITexture **ppTexture)
    void IRenderDeviceD3D11::CreateTextureFromD3DResource(ID3D11Texture3D *pd3d11Texture, ITexture **ppTexture) –
    These functions create a diligent engine texture object from the native D3D11 1D texture, 2D texture, and 3D texture correspondingly. The methods do not take texture description as all attributes are extracted from the attributes of the native object

Note that diligent engine objects will keep strong reference to the D3D11 interface. As a result, D3D11 object will not be released until Diligent Engine object is destroyed.

Example:

 

Initializing the Engine by Attaching to Existing D3D11 Device and Immediate Context

To attach diligent engine to existing D3D11 device and immediate context, use the following factory function:

The method takes a pointer to the native D3D11 device pd3d11NativeDevice, a pointer to the d3d11 immediate context pd3d11ImmediateContext, initialization parameters EngineAttribs, and returns diligent engine device interface in ppDevice, and diligent engine contexts in ppContexts. Pointer to the immediate goes at position 0. If NumDeferredContexts > 0, pointers to deferred contexts go afterwards.

The code snippet below shows how diligent engine can be attached to D3D11 device returned by Unity:

Diligent Engine keeps track of all states bound to the D3D11 immediate context (vertex and index buffers, shaders and shader resources, render targets etc.). Since main application also uses the same context, the state cache needs to be invalidated every time the control flow returns to the main application:

Initializing proxy swap chain

After initialization through AttachToD3D11Device(), diligent engine has no notion of the swap chain. There are several special commands that can be executed from the diligent engine context that require swap to be initialized in the context:

  • SetRenderTargets(0, nullptr, nullptr) – bind the default back buffer & depth buffer
  • SetViewports(1, nullptr, 0, 0) – set the viewport to match the size of the back buffer
  • ClearRenderTarget(nullptr, color) – clear the default back buffer
  • ClearDepthStencil(nullptr, ...)  – clear the default depth-stencil buffer

If the application is using any of these commands, it needs to implement ISwapChainD3D11 interface:

A possible implementation may look like this:

An instance of ProxySwapChainD3D11 should then be set in the device context after it has been initialized: