DirectX 11 Texture Sampling Samples
Overview
This section provides sample code demonstrating various texture sampling techniques and functionalities available in DirectX 11. Texture sampling is a fundamental operation in graphics programming, allowing developers to retrieve color values from textures at specified coordinates. This sample explores different sampler states, filtering methods, and addressing modes to achieve diverse visual effects.Understanding Texture Sampling in DirectX 11
In DirectX 11, texture sampling is controlled by D3D11_SAMPLER_DESC structures, which define the behavior of the sampler state. These states dictate how the GPU interpolates between texels (texture elements) and handles coordinates that fall outside the texture's boundaries.
Key Concepts:
- Filtering: Determines how texel colors are combined when a texture coordinate maps to a point between texels. Common methods include point sampling, bilinear filtering, and anisotropic filtering.
- Addressing Modes: Specifies how texture coordinates are clamped or repeated when they exceed the 0-1 range. Options include wrap, clamp, border, and mirror.
- Mipmaps: Precomputed lower-resolution versions of a texture that are used at a distance to improve performance and reduce aliasing.
Sample: Basic Texture Sampling with Bilinear Filtering
This sample demonstrates a straightforward texture loading and sampling process using bilinear filtering, which provides smooth color transitions.
// Vertex Shader Input Structure
struct VS_INPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
}
// Vertex Shader Output Structure
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
}
// Pixel Shader
Texture2D myTexture;
SamplerState mySampler;
float4 main(PS_INPUT input) : SV_TARGET
{
// Sample the texture using bilinear filtering (default)
return myTexture.Sample(mySampler, input.Tex);
}
Shader Code Explanation:
- The
VS_INPUTstructure defines the input to the vertex shader, including position and texture coordinates. - The
PS_INPUTstructure passes the transformed position and texture coordinates to the pixel shader. Texture2D myTexture;declares a 2D texture resource.SamplerState mySampler;declares a sampler state object.- The
Samplefunction on the texture object performs the actual texture lookup using the provided sampler state and texture coordinates.
Sample: Advanced Sampling with Anisotropic Filtering and Wrap Addressing
This sample explores more advanced sampling techniques. Anisotropic filtering provides higher quality at grazing angles, and the wrap addressing mode allows textures to tile seamlessly.
// Sampler State Description for Anisotropic Filtering and Wrap Addressing
D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc);
samplerDesc.Filter = D3D11_FILTER_ANISOTROPIC;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 16; // Maximum anisotropic level
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDesc.BorderColor[0] = 1.0f;
samplerDesc.BorderColor[1] = 1.0f;
samplerDesc.BorderColor[2] = 1.0f;
samplerDesc.BorderColor[3] = 1.0f;
samplerDesc.MinLOD = 0.0f;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
// Create the sampler state
ID3D11SamplerState* pSamplerState = nullptr;
HRESULT hr = pd3dDevice->CreateSamplerState(&samplerDesc, &pSamplerState);
// In the Pixel Shader:
Texture2D myTexture;
SamplerState anisotropicWrapSampler : register(s1); // Bind to a different slot
float4 main(PS_INPUT input) : SV_TARGET
{
// Sample the texture using anisotropic filtering and wrap addressing
return myTexture.Sample(anisotropicWrapSampler, input.Tex);
}
C++ Code Explanation:
D3D11_SAMPLER_DESCis populated with specific flags:D3D11_FILTER_ANISOTROPICselects anisotropic filtering.D3D11_TEXTURE_ADDRESS_WRAPensures coordinates are wrapped.MaxAnisotropycontrols the quality of anisotropic filtering.CreateSamplerStateis used to create the sampler state object on the device.- The sampler state is then bound to a shader resource slot (e.g.,
s1) and used in the pixel shader'sSamplecall.
Further Exploration
Explore other filtering modes like D3D11_FILTER_MIN_MAG_MIP_POINT (point sampling), D3D11_FILTER_MIN_MAG_MIP_LINEAR (trilinear filtering), and different addressing modes. Understanding how these settings affect visual output is crucial for achieving desired graphical fidelity and performance.