What is a Texture?
A texture in DirectX is a GPU resource that stores image data used for rendering. Textures can hold color maps, normal maps, depth buffers, and more. They are accessed by shaders through Shader Resource Views (SRV)
and can be sampled with various filtering modes.
Common Texture Types
Type | Description | Typical Use |
---|---|---|
2D Texture | Standard image stored in a 2D array. | Diffuse maps, normal maps. |
Cube Texture | Six 2D faces forming a cube. | Environment mapping, skyboxes. |
3D Texture | Volume texture with depth dimension. | Volume rendering, fog. |
Array Texture | Array of 2D textures. | Texture atlases, material variations. |
Creating a Texture (C++)
// Example: Creating a 2D texture ID3D11Texture2D* tex = nullptr; D3D11_TEXTURE2D_DESC desc = {}; desc.Width = 256; desc.Height = 256; desc.MipLevels = 1; desc.ArraySize = 1; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; desc.SampleDesc.Count = 1; desc.Usage = D3D11_USAGE_DEFAULT; desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; HRESULT hr = device->CreateTexture2D(&desc, nullptr, &tex);
Binding a Texture to a Shader
// Bind SRV to slot 0 ID3D11ShaderResourceView* srv = nullptr; device->CreateShaderResourceView(tex, nullptr, &srv); context->PSSetShaderResources(0, 1, &srv);
Texture Formats
DirectX supports many formats. Choose the appropriate one based on precision and performance.
- DXGI_FORMAT_R8G8B8A8_UNORM – Standard 8‑bit per channel.
- DXGI_FORMAT_BC7_UNORM – Compressed high‑quality.
- DXGI_FORMAT_R16G16B16A16_FLOAT – High dynamic range.
Further Reading
Explore the following sections for deeper knowledge: