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
- Dimensions: 1D, 2D, 3D, Cube, and Array textures.
- Formats: Pixel formats (e.g.,
DXGI_FORMAT_R8G8B8A8_UNORM
), compressed formats (BC1–BC7), and typeless formats. - Mipmaps: Pre‑calculated down‑scaled versions of a texture for efficient minification.
- Sampling: Filtering methods such as point, linear, anisotropic, and comparison sampling.
- Views:
ShaderResourceView
,RenderTargetView
,DepthStencilView
.
Typical Workflow
- Load image data (DDS, PNG, JPEG, etc.).
- Create a
ID3D11Texture2D
(or appropriate texture object) withD3D11_TEXTURE2D_DESC
. - Generate mipmaps if necessary using
GenerateMips
on the shader‑resource view. - Create a
ShaderResourceView
to bind the texture to the pixel shader. - Set a
SamplerState
that defines filtering and addressing modes. - Use the texture in HLSL via
Texture2D
andSamplerState
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
Dimension | DXGI Format Example | Typical Use |
---|---|---|
2D | DXGI_FORMAT_R8G8B8A8_UNORM | Diffuse maps, UI textures |
Cube | DXGI_FORMAT_BC1_UNORM | Environment maps |
3D | DXGI_FORMAT_R16G16B16A16_FLOAT | Volume textures, fog |
2D Array | DXGI_FORMAT_R8G8B8A8_UNORM_SRGB | Texture atlases, sprite sheets |