The First Triangle
After getting Donatello to display its first fuchsia viewport, the next milestone was rendering actual geometry. Until now, the graphics backend could initialize DirectX 12, record commands, clear a render target, and present the result inside the WPF editor. However, there was still no vertex data, no graphics pipeline, and no shaders capable of producing a visible object.
That changed with the implementation of Donatello's first colored triangle. Although the result is visually simple, it represents a significant step forward. The renderer can now send geometry to the GPU, process it through custom HLSL shaders, and rasterize it into the viewport. This milestone also introduced continuous rendering and viewport resizing, making the application considerably more practical to develop and test.

From a Static Viewport to Continuous Rendering
Before implementing the triangle, I extended the rendering system introduced in the previous milestone. At that point, Donatello rendered a single frame when the viewport was initialized, which was sufficient to validate the presentation pipeline but not particularly useful for an interactive editor.
To address this, I connected the rendering process to WPF's rendering events, allowing the application to continuously request new frames. I also implemented swap-chain resizing so the DirectX 12 back buffers could adapt when the native viewport changed dimensions.
The renderer now avoids unnecessary work when the viewport is hidden or the application is minimized. These changes provided a more reliable environment for testing new rendering features without altering the separation between the C# interface and the native C++ backend.
With a continuous rendering path in place, I could focus on introducing geometry.
Implementing the First HLSL Shaders
Rendering a triangle required moving beyond ClearRenderTargetView and introducing programmable shader stages.
I created an HLSL shader file containing two entry points: a vertex shader and a pixel shader. Although both functions are defined in the same file, they are compiled separately during native initialization, with compilation errors and diagnostics reported through Visual Studio.
The vertex shader receives each vertex's position and color. For this first implementation, it passes the position directly to the graphics pipeline without applying transformation matrices. There is no camera or object transformation system yet, so keeping the vertex coordinates in clip space makes the rendering process easier to validate.
The pixel shader receives the interpolated color generated during rasterization and returns it as the final pixel output. By assigning different colors to the triangle's corners, I could immediately verify that vertex attributes were being transferred and interpolated correctly.
Creating the Vertex Buffer
With the shaders available, the next step was providing actual geometry for them to process.
I defined a vertex structure in C++ containing two attributes: a three-component position and a three-component color. The triangle consists of three vertices, with red assigned to the top corner, green to the bottom-right corner, and blue to the bottom-left corner.
For this initial implementation, I created a vertex buffer using a DirectX 12 upload resource. The CPU maps the resource, copies the vertex data into GPU-accessible memory, and finishes its access once the data has been uploaded.
Each vertex occupies 24 bytes, resulting in a total of 72 bytes for the triangle. A D3D12_VERTEX_BUFFER_VIEW describes the GPU memory address, buffer size, and vertex stride, allowing the Input Assembler to retrieve the individual records correctly.
I also configured an input layout matching the HLSL vertex shader's expected attributes: three floating-point values for position followed by three for color.
This relationship between the C++ vertex structure, the input layout, and the shader inputs is essential. If these definitions do not agree, the GPU cannot interpret the vertex data as intended.

Once the vertex shader processes the three corners, the rasterizer determines which pixels are covered by the triangle. The vertex colors are then interpolated across its surface, producing the gradient visible in the final result without requiring any additional geometry.
Configuring the Graphics Pipeline
DirectX 12 requires a Pipeline State Object (PSO) to describe how geometry should be processed and rendered. Unlike the previous clear-color milestone, drawing the triangle now required configuring the shader stages, input layout, rasterization behavior, and render-target format as a complete graphics pipeline.
For this first implementation, I configured the pipeline to render filled triangles with face culling disabled, allowing both sides of the geometry to remain visible. Alpha blending and depth testing are also disabled, since neither is necessary for this initial rendering test.
The pipeline uses the same render-target format as the swap chain, ensuring that the pixel shader's output can be written into the current back buffer.
I also introduced a root signature, which defines how shaders access resources such as constant buffers and textures. In this case, the root signature contains no parameters because the triangle shaders do not require external resources. Vertex data is supplied directly through the Input Assembler.
The PSO and root signature are created during initialization and reused when recording rendering commands, avoiding the need to rebuild these objects every frame.
Recording the Triangle Draw Call
With the vertex buffer and graphics pipeline ready, I could finally connect them to Donatello's existing command submission system.
The rendering sequence still begins by transitioning the current back buffer into the render-target state and clearing it to fuchsia. However, before transitioning the resource back to the presentation state, the command list now configures the graphics pipeline and records an actual draw operation.
This involves binding the pipeline state, root signature, and vertex buffer, as well as configuring the viewport and scissor rectangle according to the current back-buffer dimensions.
Once everything is configured, the renderer issues a draw call for three vertices and one instance.

The recorded commands are submitted through the existing command queue, while fence-based synchronization ensures that the GPU completes the work before presentation.
For now, this remains a conservative synchronization model. Frames are rendered continuously, but GPU execution is still explicitly synchronized rather than allowing multiple frames to remain in flight.
Results
By the end of this milestone, Donatello could render its first colored triangle directly inside the WPF editor using the native DirectX 12 backend.
The result confirms that the main elements of the graphics pipeline are working together: vertex data is successfully uploaded, shader inputs are interpreted correctly, the rasterizer generates fragments, and the pixel shader writes interpolated colors into the render target.
The viewport also supports continuous rendering and resizing, although the triangle can appear stretched when the window's aspect ratio changes. This is expected because no camera, projection matrix, or aspect-ratio correction has been implemented yet.
At this stage, the geometry is still hardcoded for demonstration purposes. Donatello does not yet support editable meshes, transformations, lighting, or scene rendering.
Key Takeaways
This milestone helped me understand how the different stages of the DirectX 12 graphics pipeline work together to transform a small amount of vertex data into a visible image.
The most important lesson was that creating a vertex buffer and writing shaders are only parts of the process. The input layout, pipeline state, root signature, render targets, and recorded GPU commands must all be configured consistently before anything appears on screen.
Getting the triangle working also exposed an architectural issue that was less obvious during the initial rendering experiments.
At the moment, Renderer coordinates the frame but also handles demonstration-specific responsibilities such as shader compilation, triangle geometry, and graphics resource creation. Similarly, CommandContext contains logic specifically designed to record the triangle's drawing commands.
This is manageable for a first rendering test, but it would become increasingly difficult to maintain as more objects and rendering features are introduced.
The next milestone will therefore focus on restructuring these responsibilities, separating shader management, graphics pipelines, and GPU geometry from the general frame-rendering logic.
For now, Donatello has moved beyond displaying a background color. It can render its first piece of geometry, and the next challenge is turning that working experiment into a more reusable rendering architecture.



Comments