Quantcast
Channel: Diligent Graphics
Viewing all articles
Browse latest Browse all 31

Diligent Engine 2.0: Powered by Direct3D12

$
0
0

Next generation graphics APIs like Direct3D12 and Vulkan significantly reduce CPU overhead by providing low-level hardware abstraction. Diligent Engine has been updated to take advantages of the new APIs. Alpha-release of version 2.0 supports Direct3D12 and is now available on GitHub.

Several modifications to the engine were implemented to better match Direct3D12. First, all coarse-grain state objects such as Depth Stencil State, Blend State and Rasterizer State as well as shader and other states were combined into single Pipeline State object. The following example illustrates creation of a Pipeline Sate object in Diligent Engine 2.0:

 

PipelineStateDesc PSODesc;

PSODesc.GraphicsPipeline.NumRenderTargets = 1;
PSODesc.GraphicsPipeline.RTVFormats[0] = TEX_FORMAT_RGBA8_UNORM_SRGB;

// Initialize depth-stencil state
DepthStencilStateDesc &DepthStencilDesc = PSODesc.GraphicsPipeline.DepthStencilDesc;
DepthStencilDesc.DepthEnable = False;
DepthStencilDesc.DepthWriteEnable = False;

// Initialize blend state
BlendStateDesc &BSDesc = PSODesc.GraphicsPipeline.BlendDesc;
BSDesc.IndependentBlendEnable = False;
RenderTargetBlendDesc &RT0 = BSDesc.RenderTargets[0];
RT0.BlendEnable = True;
RT0.RenderTargetWriteMask = COLOR_MASK_ALL;
RT0.SrcBlend = BLEND_FACTOR_SRC_ALPHA;
RT0.DestBlend = BLEND_FACTOR_INV_SRC_ALPHA;
RT0.BlendOp = BLEND_OPERATION_ADD;
RT0.SrcBlendAlpha = BLEND_FACTOR_SRC_ALPHA;
RT0.DestBlendAlpha = BLEND_FACTOR_INV_SRC_ALPHA;
RT0.BlendOpAlpha = BLEND_OPERATION_ADD;

// Initialize rasterizer state
RasterizerStateDesc &RasterizerDesc = PSODesc.GraphicsPipeline.RasterizerDesc;
RasterizerDesc.FillMode = FILL_MODE_SOLID;
RasterizerDesc.CullMode = CULL_MODE_NONE;
RasterizerDesc.FrontCounterClockwise = True;
RasterizerDesc.ScissorEnable = True;
RasterizerDesc.AntialiasedLineEnable = False;


PSODesc.GraphicsPipeline.PrimitiveTopologyType = PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
PSODesc.GraphicsPipeline.pVS = pVertexShader;
PSODesc.GraphicsPipeline.pPS = pPixelShader;
LayoutElement TextLayoutElems[] = 
{
    LayoutElement( 0, 0, 3, VT_FLOAT32, False ),
    LayoutElement( 1, 0, 4, VT_UINT8, True ),
    LayoutElement( 2, 0, 2, VT_FLOAT32, False ),
};
InputLayoutDesc &Layout = PSODesc.GraphicsPipeline.InputLayout;
Layout.LayoutElements = TextLayoutElems;
Layout.NumElements = _countof( TextLayoutElems );

PSODesc.Name = "Example Pipeline State";
m_pDevice->CreatePipelineState(PSODesc, &m_pPSO);

Once PSO is created, it can be bound to the device context with

SetPipelineState()
  method:
m_pContext->SetPipelineState(m_pPSO);

Second big change deals with the resource binding model. In Diligent Engine 1.0, resources were bound directly to shaders, which is the old-API style. To better fit next-generation APIs, Diligent Engine 2.0 implements a new binding model. The model introduces Shader Resource Binding object and classifies shader variables into one of three categories:

  • Static variables are constant across all shader instances. They must be set once directly through
    IShader::BindResources()
      or through the shader variable interface queried from the shader.
  • Mutable variables are constant across shader resource bindings instance. They must be set once through
    IShaderResourceBinding::BindResources()
      or through the shader variable interface queried from the resource binding. Mutable variables cannot be set through
    IShader
      interface.
  • Dynamic variables can be set multiple times for every instance of shader resource binding. They cannot be set through
    IShader
     interface.

Shader Resource Binding objects are created by the Pipeline State:

m_pPSO->CreateShaderResourceBinding(&m_pSRB);

To set resources within a shader resource binding,

IShaderResourceBinding::BindResources()
 method can be used in the same way it was used in Diligent Engine 1.0. Alternatively, specific variable can be queried and initialized:
m_pSRB->GetVariable(SHADER_TYPE_PIXEL, "Tex")->Set(m_pTextureSRV);

In Diligent Engine 2.0, resources are bound explicitly to the graphics pipeline by the 

CommitShaderResources()
  method of the device context. The method takes shader resource binding as an argument:
m_pContext->CommitShaderResources(m_pSRB);

Diligent Engine 2.0 also implements OpenGL and Direct3D11 back-ends. Alpha release is only available on Windows platform. Direct3D11 back-end is very thoroughly optimized and has very low overhead compared to native implementation. Direct3D12 implementation, to the contrary, is preliminary and not yet optimized.


Viewing all articles
Browse latest Browse all 31

Trending Articles