## Texture sampling

### 1. HLSL texture sampling representation

We know that Texture2D objects are used to represent textures in effect files. However, there is also a SamplerState object related to textures. It is used to describe how to use filters to access texture resources. Here are some examples of it:

```
//use linear filtering on multiplication, reduction, and multi-level progressive textures.
SamplerState mySampler0
{
Filter = MIN_MAG_MIP_LINEAR;
};
//use linear filtering for reduction, point filtering for multiplication and multi-level progressive textures.
SamplerState mySampler1
{
Filter = MIN_LINEAR_MAG_MIP_POINT;
};
//use point filtering for reduction, linear filtering for multiplication, and point filtering for multi-level progressive textures.
SamplerState mySampler2
{
Filter = MIN_POINT_MAG_LINEAR_MIP_POINT;
};
//use anisotropic filtering on multiplication, reduction, and multi-level progressive textures.
SamplerState mySampler3
{
Filter = ANISOTROPIC;
MaxAnisotropy = 4;
};
```

Note that for anisotropic filtering, we must specify the maximum value of anisotropy, which is between 1 and 16. Large values require more resources, but the effect is better. You can guess some other flag values from these examples, or you can search for D3D11 in the SDK documentation_ FILTER enumeration type. We will soon see other properties related to the sampler, but for our first demonstration program, the current content is already sufficient.

Now, each pixel passed into the pixel shader has a pair of texture coordinates, and we use the following syntax to complete the actual texture sampling work:

```
Texture2D gDiffuseMap;
struct VertexOut
{
float4 posH : SV_POSITION;
float3 posW : POSITION;
float3 normalW : NORMAL;
float2 Tex : TEXCOORD;
};
SamplerState samAnisotropic
{
Filter = ANISOTROPIC;
MaxAnisotropy = 4;
};
float4 PS(VertexOut pin, uniform int gLightCount) : SV_Target
{
//extracting colors from textures
float4 texColor = gDiffuseMap.Sample(samAnisotropic, pin.Tex);
...
```

As can be seen, we use the Texture2D:: Sample method to sample a texture. Pass the SamplerState object through the first parameter, and pass the texture coordinates (u, v) of pixels through the second parameter. This method returns the interpolated color at point (u, v) from the texture map using the filtering method specified by the SamplerState object.

### 2. DirectX11 texture sampling representation

Note: The HLSL type SamplerState corresponds to the ID3D11SamplerState interface. The sampling status can also be set in the application using the ID3DX11EffectSamplerVariable:: SetSampler method. See D3D11_ SAMPLER_ DESC and ID3D11Device:: CreatSamplerState. Like the rendering state, a sampling state must be created during the initialization phase.