45 lines
2.8 KiB
Markdown
45 lines
2.8 KiB
Markdown
---
|
|
title: "Sampler"
|
|
date: 2021-01-25T20:48:35-08:00
|
|
weight: 3
|
|
---
|
|
|
|
You might be familiar with the concept of "sampling" in music, where artists take a small segment of recorded music and recontextualize it in another work. Similarly, in graphics applications, sampling refers to the process of looking up segments of texture information in a shader so the data can be used.
|
|
|
|
Samplers tell the renderer how upscaling or downscaling scenarios should be filtered. They can also cause the texture to repeat or mirror itself, among other possibilities.
|
|
|
|
To create a sampler, define a `SamplerState` struct:
|
|
|
|
```cs
|
|
var mySamplerState = new SamplerState
|
|
{
|
|
MinFilter = Filter.Linear,
|
|
MagFilter = Filter.Linear,
|
|
MipmapMode = SamplerMipmapMode.Linear,
|
|
AddressModeU = SamplerAddressMode.Repeat,
|
|
AddressModeV = SamplerAddressMode.Repeat,
|
|
AddressModeW = SamplerAddressMode.Repeat,
|
|
CompareEnable = false,
|
|
AnisotropyEnable = false,
|
|
MipLodBias = 0f,
|
|
MinLod = 0,
|
|
MaxLod = 1000
|
|
};
|
|
|
|
var mySampler = new Sampler(GraphicsDevice, mySamplerState);
|
|
```
|
|
|
|
There are three kinds of sample filters: Nearest, Linear, and Cubic. Nearest is the cheapest and doesn't do any kind of filtering. This filter is ideal for upscaling pixel games - Linear or Cubic will make your pixel art look terrible. Cubic is the most expensive filter but upscales and downscales in decent quality.
|
|
|
|
MinFilter describes which filter to use when the result image is smaller than the sampled image - MagFilter, likewise, is used when the result image is larger.
|
|
|
|
[Anisotropic filtering](https://en.wikipedia.org/wiki/Anisotropic_filtering) can be used in conjunction with mipmaps to reduce aliasing and blur on obliquely-rendered textures. Explaning this is beyond the scope of this tutorial but I encourage you to look into the technique if you are planning to make 3D games.
|
|
|
|
When you look up pixels using a sampler, you generally look them up using coordinates in the range of [0, 1]. This coordinate space is often referred to as "texture coordinate space" or "UV space". SamplerAddressMode refers to the behavior that will occur if the texture is sampled *outside* of [0, 1] coordinates. ClampToEdge will just take the pixel at the nearest edge. Repeat will loop the image, and MirroredRepeat will mirror the image. ClampToBorder will return a predefined border color.
|
|
|
|
U-coordinates are horizontal, V-coordinates are vertical, and W-coordinates correspond to depth. You can use different address spaces for each of these directions.
|
|
|
|
The same sampler can be used in conjunction with as many different textures as you like. We will discuss this more when we get into "binding" resources using command buffers.
|
|
|
|
There are some common `SamplerState`s that are provided for you, for example `SamplerState.PointClamp`. Have a look at the `SamplerState` definition to see them all.
|