Textures

Textures are used in order to specify a color buffer. The concept of textures and mipmaps are beyond the scope of this article.

The texture is created using CreateTexture2D. This will be created using D3D11_USAGE_IMMUTABLE. This indicates the texture will never be edited by the runtime.

D3D11_SUBRESOURCE_DATA subresData[16];
for ( uint32_t j = 0; j < imageDesc->mipCount; j++ )
{
    subresData[j] = (D3D11_SUBRESOURCE_DATA){
        .pSysMem     = pixelData + imageDesc->mips[j].offset,
        .SysMemPitch = imageDesc->mips[j].width * sizeof ( uint32_t )
    };
}

ID3D11Texture2D* tex;
result = renderer->device->lpVtbl->CreateTexture2D (
    renderer->device,
    &(D3D11_TEXTURE2D_DESC){
        .Width      = imageDesc->width,
        .Height     = imageDesc->height,
        .MipLevels  = imageDesc->mipCount,
        .ArraySize  = 1,
        .Format     = DXGI_FORMAT_R8G8B8A8_UNORM,
        .Usage      = D3D11_USAGE_IMMUTABLE,
        .BindFlags  = D3D11_BIND_SHADER_RESOURCE,
        .CPUAccessFlags = 0,
        .SampleDesc.Count = 1,
    },
    subresData,
    &tex
);
if ( !SUCCEEDED ( result ) )
    RETURN_ERROR(-1, "CreateTexture2D failed (0x%08X)", result );

renderer->device->lpVtbl->CreateShaderResourceView (
    renderer->device,
    (ID3D11Resource*)tex,
    &(D3D11_SHADER_RESOURCE_VIEW_DESC){
        .Format        = DXGI_FORMAT_R8G8B8A8_UNORM,
        .ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D,
        .Texture2D = {
            .MipLevels = imageDesc->mipCount,
        }
    },
    &renderer->tex[i]
);
if ( !SUCCEEDED ( result ) )
    RETURN_ERROR(-1, "CreateShaderResourceView failed (0x%08X)", result );

The shader resource view is the object used in the rendering process rather than the ID3D11Texture2D object. Similar to the ID3D11DepthStencilView and ID3D11RenderTargetView, this is the object used in order to interpret the resource. As such, not all properties need to be identical to the texture itself. In addition, the texture itself is generally not required, but the shader resource view is.