Separating Content from Rendering
In the previous milestone, Donatello successfully rendered its first coloured triangle using a custom DirectX 12 graphics pipeline. Although the result was simple, it provided a working foundation for testing shaders, vertex buffers, pipeline state objects, and GPU command submission.
However, implementing that first triangle also exposed an architectural problem. The renderer was responsible not only for coordinating frames but also for compiling the demonstration shaders, defining the triangle's vertices, and creating the graphics resources needed to draw them.
This approach was sufficient for an initial rendering experiment, but it would quickly become difficult to maintain as the application grew.
Before introducing transformations or more complex geometry, I decided to refactor the rendering backend, separating demonstration content from the infrastructure responsible for rendering it.
The objective was to preserve the existing visual result while establishing clearer responsibilities, explicit resource ownership, and a safer shutdown sequence.
Separating Demo Content from Frame Rendering
The first step was identifying which responsibilities belonged to the renderer and which were specific to the triangle demonstration.
Previously, Renderer handled almost everything required to produce the image. It created the graphics resources, compiled the shaders, configured the pipeline, and coordinated the commands used to draw the triangle.
I wanted the renderer to become independent of the content being displayed.
To achieve this, I introduced DemoContent, a dedicated class responsible for preparing the triangle's shaders, vertex data, and graphics resources. Rather than asking the renderer to know how the triangle is constructed, DemoContent now provides a RenderItem describing what should be drawn.
A render item references the graphics pipeline, GPU mesh, and primitive topology required for a draw operation.
The renderer receives these items, prepares the frame, records their drawing commands, and submits the resulting work to the GPU. It no longer needs to know the shader filename or the triangle's vertex coordinates.
[Figure 1 — Rendering Architecture After Refactoring]
The existing native API connects both systems through DonatelloGraphics, which contains the renderer and demonstration content. This allowed me to reorganize the C++ backend without changing the public functions already used by the C# WPF application.
An important distinction is that DemoContent remains temporary demonstration functionality. It is not intended to represent Donatello's future document or scene system, which will require its own data structures and responsibilities.
Introducing Dedicated Graphics Resource Classes
After separating the demonstration from the renderer, I focused on improving resource ownership.
DirectX 12 requires explicit management of GPU resources, making it particularly important to know which objects are responsible for their creation and lifetime. To make these responsibilities clearer, I introduced three dedicated classes.
ShaderCompiler
Shader compilation was extracted into ShaderCompiler, which handles individual HLSL compilation requests.
The caller provides the shader file, entry point, target profile, and compilation options. The compiler then returns the resulting bytecode, diagnostic information, and compilation status.
This keeps shader compilation independent of any particular demonstration or rendering feature.
GraphicsPipeline
I introduced GraphicsPipeline to encapsulate the Pipeline State Object and its compatible root signature.
Instead of creating these resources directly inside Renderer, the pipeline class now receives the necessary configuration and manages their lifetime.
An important part of this implementation was handling partial initialization failures. Both DirectX 12 objects are created successfully before being committed to the class, while temporary COM references ensure that resources are released correctly if initialization fails.
GpuMesh
The third class, GpuMesh, manages the vertex buffer, its corresponding vertex buffer view, and the number of vertices available for rendering.
The class receives vertex data and copies it into a GPU-accessible buffer during initialization. However, it does not need to understand what those bytes represent.
For example, the triangle currently uses positions and colors, but their interpretation belongs to the graphics pipeline's input layout rather than the mesh resource itself.
This separation makes the GPU buffer management less dependent on a particular vertex format.
Making Resource Ownership Explicit
One of the most important aspects of the refactoring was distinguishing between owning a resource and referencing it during rendering.
DonatelloGraphics contains both Renderer and DemoContent, while each subsystem manages the resources associated with its responsibilities.
The renderer owns the infrastructure required to execute frames, including the device-related resources, command queue, swap chain, and command context.
Meanwhile, DemoContent owns the shaders, graphics pipeline, and GPU mesh used by the triangle.
[Figure 2 — Graphics Resource Ownership]
A RenderItem does not take ownership of these resources. It simply contains references to the objects required for a draw operation.
The renderer receives render items through a std::span, allowing it to process an existing sequence without copying its contents.
This introduces an important lifetime consideration. Although a render item only needs to remain available while its commands are being recorded, the GPU resources referenced by those commands must remain valid until execution has finished.
Understanding this distinction was essential when reviewing the application's shutdown process.
Generalizing Command Recording
Another limitation of the previous implementation was that CommandContext contained logic specifically designed to draw the demonstration triangle.
That meant a low-level rendering class knew about a particular piece of content.
I refactored command recording into three operations:
Begin Frame: Prepares the current back buffer and clears the render target.
Draw: Binds the supplied graphics pipeline and mesh, then records the corresponding draw operation.
End Frame: Transitions the back buffer for presentation and closes the command list.
With this structure, CommandContext no longer contains a hardcoded draw call for three vertices. Instead, it uses the vertex count provided by the mesh.
The renderer can now process multiple render items within the same frame or receive an empty sequence without modifying the underlying command-recording logic.
A recording flag also checks that these operations are called in the correct order. This validates command recording, although it does not indicate whether the GPU has finished executing the submitted work.
Handling GPU Resources During Shutdown
Separating resource ownership revealed another important issue: the order in which the application releases its graphics resources.
DirectX 12 executes submitted commands asynchronously, so releasing resources while the GPU is still using them can lead to invalid memory access or other rendering problems.
The original approach relied on synchronization performed during renderer destruction. However, the native context declares Renderer before DemoContent, meaning C++ destroys the demonstration resources first.
Waiting in the renderer's destructor would therefore happen too late. To address this, I moved the final GPU synchronization into the DonatelloGraphics destructor body, before either member is destroyed.
The application now follows a deliberate shutdown sequence:
[Figure 3 — Safe Graphics Shutdown Sequence]
WPF first stops requesting new frames. The native context then waits for outstanding GPU work to finish before releasing the demonstration resources and rendering infrastructure. Finally, the hosted viewport is destroyed.
COM resource ownership handles the actual release of DirectX 12 objects, while fence synchronization ensures that those objects are no longer being used by the GPU during normal shutdown.
This change made the graphics backend's lifetime management considerably easier to reason about.
Results
The refactoring was completed without changing the visible rendering result. Donatello still displays the same colored triangle, but the code responsible for producing it is now divided into more clearly defined systems.
To verify the changes, I tested the application in Debug and Release configurations, including viewport resizing and application shutdown.
I also performed a useful test by passing an empty render-item sequence to Renderer. The application successfully cleared and presented the fuchsia background without drawing the triangle.
Restoring the render item brought the triangle back. This confirmed that the renderer could execute its frame lifecycle independently of the demonstration content, which was one of the main objectives of the refactoring.
Key Takeaways
This milestone reinforced something I consider particularly important in engine development: getting a feature working is only part of the challenge. The architecture surrounding that feature determines how easily the system can evolve.
By separating rendering infrastructure from demonstration content, I gained a better understanding of resource ownership, non-owning references, object lifetimes, and the relationship between CPU-side data and asynchronous GPU execution.
The shutdown issue was especially valuable because it demonstrated that automatic resource management alone is not sufficient in DirectX 12. Even when C++ objects release their resources correctly, the application must ensure that the GPU has finished using them.
Donatello's rendering capabilities remain intentionally limited. The GPU mesh currently uses a single non-indexed upload vertex buffer, and the renderer still targets one colour buffer.
Nevertheless, the underlying structure is now better prepared for additional functionality.
The next milestone will introduce transformations, building on a rendering architecture where geometry and pipelines can be supplied independently of frame coordination.
The triangle may look exactly the same, but the system behind it is now much more organized, maintainable, and ready to grow.




Comments