State management

When rendering in DirectX 11, we need to instruct the driver and hardware in what way the geometry is to be rendered. A large chunk of the fixed-function functionality can be tweaked in order to render geometry the way we want it to be rendered.

The fixed-function state is grouped in 3 objects:

  • ID3D11BlendState: Responsible for controlling whether and how pixels are to be be blended with previous pixels in the same location. This can be used in order to render transparent objects.
  • ID3D11DepthStencilState: Responsible for controlling how pixels are affected by and affect the depth- and stencil buffers,
  • ID3D11RasterizerState: Responsible for controlling how geometry is to be converted to pixels. Indicates whether geometry should be filled or wireframe, whether the front faces and/or back faces should be rendered and a bias to the depth.

These fixed function state objects can be created multiple times with a different set of properties, and can be swapped in between draw operations. For the purposes of the application being described here, only a single state object of each type will be created and be set at the start of every frame.

result = renderer->device->lpVtbl->CreateBlendState (
    renderer->device,
    &(D3D11_BLEND_DESC){
        .RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL,
    },
    &renderer->bs
);
if ( !SUCCEEDED ( result ) )
    RETURN_ERROR(-1, "CreateBlendState failed (0x%08X)", result );
result = renderer->device->lpVtbl->CreateDepthStencilState (
    renderer->device,
    &(D3D11_DEPTH_STENCIL_DESC){
        .DepthEnable = TRUE,
        .DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL,
        .DepthFunc = D3D11_COMPARISON_LESS_EQUAL
    },
    &renderer->ds
);
if ( !SUCCEEDED ( result ) )
    RETURN_ERROR(-1, "CreateDepthStencilState failed (0x%08X)", result );
result = renderer->device->lpVtbl->CreateRasterizerState (
    renderer->device,
    &(D3D11_RASTERIZER_DESC){
        .FillMode = D3D11_FILL_SOLID,
        .CullMode = D3D11_CULL_FRONT,
    },
    &renderer->rs
);
if ( !SUCCEEDED ( result ) )
    RETURN_ERROR(-1, "CreateRasterizerState failed (0x%08X)", result );