Grouping D3D11 Pipeline State
After using Hieroglyph 3 for a while now, and especially after doing some refactoring of the sample programs, I have had a chance to revisit some of the design choices that I made while building the library. Some things work extraordinarily well, and some things not so much... But after gaining more experience and learning more about software engineering, I have really enjoyed looking at how I did certain things - and more importantly to figure out how to improve on some of those designs. This time around I will be talking about how the pipeline state is handled, how I set up the pipeline for rendering, and then how the pipeline is actually executed to do the desired drawing.
When it comes down to it, there are basically three different areas that comprise the overall Direct3D 11 rendering pipeline state. Starting at the entry to the pipeline, there is state that provides input data into the pipeline. Examples of this are the input layout, primitive type selection, vertex buffers, and index buffers. They more or less only put data into the system, either literally as a data source (in the case of the vertex / index buffers) or as a configuration (in the case of input layout and primitive type). This group of states reside in the input assembler, which seems appropriately named.
Then you have what I would call the actual pipeline setup - all of the states and configuration for the pipeline stages from the vertex shader through to the pixel shader. This includes all of the programmable stages, which have shader programs, samplers, constant buffers, plus other resources added as input with shader resource views. In addition, there is the fixed pipeline stages as well, the most important of which is the rasterizer stage and its configuration. I also group the output merger's depth and blend configurations in this pipeline state. This group controls all of the processing of the data that flows through the pipeline.
Finally, there is pipeline state which configures the output of the pipeline - which is more or less the output merger stage. This is essentially a collection of output render targets, depth targets, and perhaps unordered access views. Since it receives the output data from the pipeline, I think of this group of states as the logical output of the pipeline.
Hieroglyph 3 and Rendering
In Hieroglyph 3, I let render views configure the output of the pipeline. The pipeline setup is provide by my material system, and the input configuration is provided by the geometry class. There is a semi-convoluted path to get all of the state set properly, but in the end these three classes are responsible for the configuration of the whole pipeline.
After it has been configured, it is then time to actually do some rendering. In the beginning of the library development, I implemented a simple draw call that took a reference to a geometry object, a render effect (from the material system), and then called a DrawIndexed(...) pipeline invocation. This worked great for a long time, especially since I was only using basic input into the pipeline, which was either manually generated and loaded into a geometry object, or loaded from a Milkshape3D file.
While we were developing the samples for Practical Rendering and Computation with D3D11, we started to need fancier types of draw calls, and also fancier input state setups. D3D11 isn't shy about giving you options to get your data into the pipeline, and also has a good variety of draw calls to use. With indexed, instanced, and indirect flavors for draw (and dispatch calls too, for that matter), it was kind of difficult to fit a bunch of different capabilities into a monolithic geometry class - configuring and executing all these different types of draw calls was becoming untenable.
In the end, we ended up adding more draw calls that could be manually configured instead of having the simple geometry class take care of everything. This made the system functional, but still not ideal, and certainly not a pretty design point...
The New PipelineExecutorDX11 Interfaces
To remedy this point, I have replaced the monolithic geometry class with a PipelineExecutorDX11 base class reference. This interface simply provides one "Execute" method, with provides parameters to the pipeline and parameter system classes. In the end, this is intended to delegate the entire pipeline input configuration to the PipelineExecutorDX11 subclass. After configuring the pipeline input, the execute method also provides its own draw call. This lets each implementation of the PipelineExecutorDX11 class decide on its own which sets of state and draw call will best fit for its type of rendering action.
Even though it seems like a very clean-cut design, it has really taken some time to get things converted over. I now have the GeometryDX11 class converted to be a subclass of PipelineExecutorDX11, and approximately 90% of the drawing in the entire engine and all of its samples are covered by this method. I am now going through the remaining 10% of drawing and creating new subclasses to handle the special case rendering that is going on. In the end, this will lead to more robust and maintainable code, and also make the entire drawing system easily extensible.
For example, I intend to add an immediate mode style geometry object that can collect simple draw calls and then collate them into a single buffer to be drawn all at once. This is the same style of rendering that is used in Processing, and it provides a simple but effective rendering model for small scale projects (this is more or less the pre-programmable pipeline rendering system). Before this design update, doing this would have meant trying to make a converter class to put the data into a GeometryDX11 instance, which would be less than optimal...
Overall, I am quite happy with the progress so far, and I hope to have a stable version available fairly soon. Keep an eye on the repository over the next week or so for an update!