From cube, to the graphcis API
Rendering Hardware Interface
It is now time to use the RHI command. RHI stands for 'Rendering Hardware Interface'. It abstracts the real graphics API of each platform, like DirectX or Vulkan. You can treat it as a simple mapping to the graphics API. For example, RHIDrawIndexedPrimitive
can be treated as DrawIndexedInstanced
.
Do you remember how to render a triangle when you learned DirectX 11 or 12?
We need to set up the render pipeline state, set the shaders and parameters, and use DrawIndexedInstanced
to dispatch a draw call.
If you forget, just find a hello triangle tutorial, for example:
From Mesh Draw Command to RHI
We need to perform almost the same action for each FMeshDrawCommand
. However, we should not directly call the API. Instead, we should add the command to a RHICommandList
. This list will be sent to and executed by the RHI thread at a later time.
In our one cube, and the depth pass, it generates the following commands:
So our story becomes:
- The render thread takes the rendering data, including both the runtime data (render proxies from components) and the common data (render data from assets), and uses it to render the image.
- For each
FStaticMeshSceneProxy
: - Retrieve many
FMeshBatch
objects. - For each
FMeshBatch
: - Build mesh draw commands for each pass.
- For each pass, merge all the draw commands that belong to this pass.
- For each pass:
- Execute the related
FMeshDrawCommand
list. - Convert the mesh draw command into a set of
RHICommand
- Add those commands to RHI command list
- Send the list to RHI thread
- The RHI thread take the command list, and execute.
So let me show a summary image to conclude from FMeshBatch
to FMeshDrawCommand
and then to RHICommand
.
A little more about RHI
RHI is not exclusive to the Unreal Engine; many engines implement their own RHI layer. Additionally, there are RHI implementations that serve as middleware, such as NvRHI.