DirectX Graphics – Textures Overview

What is a Texture?

A texture is an image data resource that can be bound to the graphics pipeline and sampled by shaders. Textures provide surface detail, color information, and can be used for a variety of effects such as normal mapping, displacement, and environment mapping.

Key Concepts

Typical Workflow

  1. Load image data (DDS, PNG, JPEG, etc.).
  2. Create a ID3D11Texture2D (or appropriate texture object) with D3D11_TEXTURE2D_DESC.
  3. Generate mipmaps if necessary using GenerateMips on the shader‑resource view.
  4. Create a ShaderResourceView to bind the texture to the pixel shader.
  5. Set a SamplerState that defines filtering and addressing modes.
  6. Use the texture in HLSL via Texture2D and SamplerState objects.

Sample Code

// Load a DDS file and create a texture
ID3D11Device* device = /* ... */;
ID3D11DeviceContext* context = /* ... */;
ID3D11ShaderResourceView* textureView = nullptr;

HRESULT hr = D3DX11CreateShaderResourceViewFromFile(
    device,
    L"DiffuseMap.dds",
    nullptr,
    nullptr,
    &textureView,
    nullptr);

if (SUCCEEDED(hr))
{
    // Bind the texture and a sampler to the pixel shader
    ID3D11SamplerState* sampler = nullptr;
    D3D11_SAMPLER_DESC sampDesc = {};
    sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.MaxAnisotropy = 1;
    sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    sampDesc.MinLOD = 0;
    sampDesc.MaxLOD = D3D11_FLOAT32_MAX;

    device->CreateSamplerState(&sampDesc, &sampler);
    context->PSSetShaderResources(0, 1, &textureView);
    context->PSSetSamplers(0, 1, &sampler);
}

Further Reading

Reference Table

DimensionDXGI Format ExampleTypical Use
2DDXGI_FORMAT_R8G8B8A8_UNORMDiffuse maps, UI textures
CubeDXGI_FORMAT_BC1_UNORMEnvironment maps
3DDXGI_FORMAT_R16G16B16A16_FLOATVolume textures, fog
2D ArrayDXGI_FORMAT_R8G8B8A8_UNORM_SRGBTexture atlases, sprite sheets