Graphics Reference
In-Depth Information
sampler state must be declared as a SamplerState object in HLSL, which is mapped to a
sampler register (s0 through s15) in the same way constant buffers and resource objects
are mapped to their register types. The index of the register corresponds to the slot passed
to the *SSetSamplers methods available on the device context. A declared SamplerState
object can then be passed to the Sample method, which will in turn use the specified sam-
pler states for filtering. Listing 6.17 contains a simple pixel shader that demonstrates call-
ing Sample on a Texture2D using a sampler state.
SamplerState LinearSampler : register(s0);
Texture2D ColorTexture : register(t0);
float4 PSMain( in float2 TexCoord : TEXCOORD ) : SV_Target
{
return ColorTexture. Sample( LinearSampler, TexCoord );
}
Listing 6.17. Sampling a texture in a pixel shader.
When Sample is called on a texture that contains multiple mip-map levels, the mip-
map level is automatically selected based on the screen-space derivatives (also known
as gradients) of the texture coordinates. This is why Sample is only available in pixel
shaders, and only outside of dynamic loop or branch constructs. To use texture sampling
in other shader stages, or inside a dynamic branch/loop in a pixel shader, other variants
of the Sample method are available, which allow the gradients or the mip level to be ex-
plicitly specified. These are SampleGrad and SampleLevel, respectively. A third method,
SampleBias, works like Samplelevel with the addition of a bias value.
The return type of a sample method depends on the DXGI_FORMAT specified when
creating the shader resource view bound for the texture object. Thus, DXGI_FORMAT_
R32G32B32A32_FL0AT will return a float4, DXGI_FORMAT_R32G32_UINT will return a
uint2, and DXGI_F0RMAT_R8G8B8A8_UN0RM will return a float4 .
SampleCmp Methods
Rather than directly returning a texture value, SampleCmp returns the result of comparing
the sampled value against a value passed to the method as the CompareValue parameter.
The returned value is equal to 1.0 if the comparison passes, and 0.0 if the comparison fails.
This makes the method quite natural for implementing shadow mapping techniques.
The inequality used in the comparison is specified in the ComparisonFunc member of
the D3D11_SAMPLER_DESC structure used to create the sampler state passed to the method.
Note that the filter specified for the sampler state must be one of the COMPARISON values of
the D3D11_FILTER enumeration. Also, the sampler state declared in HLSL must have the
SamplerComparisonState type if it is to be passed to SampleCmp. If a linear filtering mode
Search WWH ::




Custom Search