DirectX12- Pipeline

1. Pipeline

1.1 What is a Pipeline

  • The pipelines in DirectX12 are mainly divided into two categories, Graphic Pipeline And Compute Pipeline
  • The definition of the Graphic Pipeline is as follows: A graphics pipeline is the sequential flow of data inputs and outputs as the GPU renders frames . To expand on, DirectX12 divides the entire rendering process into several stages, each responsible for a fixed function. Therefore, the entire process can be summarized as one Pipeline (assembly line).
  • At first, GPUs were specifically designed for rendering graphics, but later specialized computing GPUs were developed. DirectX12 defined another set of pipelines, which is Compute Pipeline This pipeline only contains the Compute Shader (CS) module.

1.2 Pipeline Detailed Process

  • The detailed flowcharts for Graphic Pipeline and Compute Pipeline can be found on the Microsoft official website and the Spec official website of DirectX11.3.

Adding some cool knowledge: Why does DirectX12 refer to the Spec of DirectX11.3? Because Microsoft has been upgrading DirectX11, the Pipeline model of DirectX11.3 is now consistent with DirectX12 and can be seen as a transitional version of DirectX12.

  • We can clearly see that, as mentioned earlier, the Graphic Pipeline is specifically used for graphic rendering, just like a factory assembly line. It is divided into IA, VS, HS, TS, DS, GS, RS, PS, OM, SO, and each stage has detailed definitions in its functional diagrams. Compute Pipeline is specifically used for numerical operations and only contains one CS Stage.

1.3 Description of Shaders in Pipeline

1.3.1 Graphic Pipeline


.

  • As mentioned earlier, the Shader in the Graphic Pipeline (which, like Stage, refers to a stage in the pipeline) includes IA, VS, HS, TS, DS, GS, RS, PS, OM, and SO.
  • User programmable Shaders( Programmed Shader )Mainly VS, PS, DS, HS, GS These shaders can be calculated using HLSL (High Level Shader Language), and users can customize the calculation process for Vertex Input.

Additional knowledge point: When it comes to Vertex Input, in graphics driver frameworks such as DirectX, OpenGL, and Vulkan, graphics are constructed from n Vertex (vertices). For example, by entering 3 vertices Vertex0, Vertex1, and Vertex2, and specifying that the Primitive Topology between them is a triangle, a triangle can be drawn using these 3 vertices. So, and so on, divide each face of the 3D shape into n triangles, and input their Vertex data into the DirectX12 shape in a certain order to draw this 3D shape.

  • The rest also includes IA, TS, SO, RS, OM These Shaders, also known as Fixed Shaders, cannot have a calculation process set and can only use the Pipeline Stage Object structure to set some specific states.
  • The setting of the Graphic Pipeline in DirectX12 is mainly achieved through the ID3D12Device::CreateGraphicsPipelineState() interface, which uses the D3D12_GRAPHICS_PIPELINE_STATE_DESC structure. This structure contains the programmable Shader mentioned earlier, such as VS, PS, DS, HS, GS
  • And several shaders that can set the status, StreamOutput can set SO, RasterizerState can set RS, InputLayout, IBStripCutValue, and PrimitiveTopologyType can set IA, and RTVFormats can set the data format for the OM output stage.
  typedef struct D3D12_GRAPHICS_PIPELINE_STATE_DESC {
ID3D12RootSignature                *pRootSignature;
D3D12_SHADER_BYTECODE              VS;
D3D12_SHADER_BYTECODE              PS;
D3D12_SHADER_BYTECODE              DS;
D3D12_SHADER_BYTECODE              HS;
D3D12_SHADER_BYTECODE              GS;
D3D12_STREAM_OUTPUT_DESC           StreamOutput;
D3D12_BLEND_DESC                   BlendState;
UINT                               SampleMask;
D3D12_RASTERIZER_DESC              RasterizerState;
D3D12_DEPTH_STENCIL_DESC           DepthStencilState;
D3D12_INPUT_LAYOUT_DESC            InputLayout;
D3D12_INDEX_BUFFER_STRIP_CUT_VALUE IBStripCutValue;
D3D12_PRIMITIVE_TOPOLOGY_TYPE      PrimitiveTopologyType;
UINT                               NumRenderTargets;
DXGI_FORMAT                        RTVFormats[8];
DXGI_FORMAT                        DSVFormat;
DXGI_SAMPLE_DESC                   SampleDesc;
UINT                               NodeMask;
D3D12_CACHED_PIPELINE_STATE        CachedPSO;
D3D12_PIPELINE_STATE_FLAGS         Flags;
} D3D12_GRAPHICS_PIPELINE_STATE_DESC;

1.3.2 Compute Pipeline


.

  • DirectX12 sets up Compute Pipeline mainly through the ID3D12Device::CreateComputePipelineState() interface, which uses the D3D12_COMPUTE_PIPELINE_STATE_DESC structure
typedef struct D3D12_COMPUTE_PIPELINE_STATE_DESC {
ID3D12RootSignature         *pRootSignature;
D3D12_SHADER_BYTECODE       CS;
UINT                        NodeMask;
D3D12_CACHED_PIPELINE_STATE CachedPSO;
D3D12_PIPELINE_STATE_FLAGS  Flags;
} D3D12_COMPUTE_PIPELINE_STATE_DESC;
  • Compute Pipeline is a pipeline dedicated to performing numerical operations, which only contains one CS Shader. The Shader is written by the user using HLSL and then handed over to the D3DCompileFromFile() interface for compilation into binary. Finally, it is given to the D3D12_COMPUTE_PIPELINE_STATE_DESC structure above.