1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
use crate::ToGlEnum;
use crate::gl;
/// Function to use for out-of-bounds samples.
///
/// This is how GL must handle samples that are outside the texture.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum SamplerWrapFunction {
/// Samples at coord `x + 1` map to coord `x`.
Repeat,
/// Samples at coord `x + 1` map to coord `1 - x`.
Mirror,
/// Samples at coord `x + 1` map to coord `1`.
Clamp,
/// Use texture border.
BorderClamp,
/// Same as Mirror, but only for one repetition,
MirrorClamp
}
impl ToGlEnum for SamplerWrapFunction {
#[inline]
fn to_glenum(&self) -> gl::types::GLenum {
match *self {
SamplerWrapFunction::Repeat => gl::REPEAT,
SamplerWrapFunction::Mirror => gl::MIRRORED_REPEAT,
SamplerWrapFunction::Clamp => gl::CLAMP_TO_EDGE,
SamplerWrapFunction::BorderClamp => gl::CLAMP_TO_BORDER,
SamplerWrapFunction::MirrorClamp => gl::MIRROR_CLAMP_TO_EDGE,
}
}
}
/// The function that the GPU will use when loading the value of a texel.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum MagnifySamplerFilter {
/// The nearest texel will be loaded.
Nearest,
/// All nearby texels will be loaded and their values will be merged.
Linear,
}
impl ToGlEnum for MagnifySamplerFilter {
#[inline]
fn to_glenum(&self) -> gl::types::GLenum {
match *self {
MagnifySamplerFilter::Nearest => gl::NEAREST,
MagnifySamplerFilter::Linear => gl::LINEAR,
}
}
}
/// The function that the GPU will use when loading the value of a texel.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum MinifySamplerFilter {
/// The nearest texel will be loaded.
///
/// Only uses the main texture, mipmaps are totally ignored.
Nearest,
/// All nearby texels will be loaded and their values will be merged.
///
/// Only uses the main texture, mipmaps are totally ignored.
Linear,
/// The nearest texel of the nearest mipmap will be loaded.
NearestMipmapNearest,
/// Takes the nearest texel from the two nearest mipmaps, and merges them.
LinearMipmapNearest,
/// Same as `Linear`, but from the nearest mipmap.
NearestMipmapLinear,
/// Same as `Linear`, but from the two nearest mipmaps.
LinearMipmapLinear,
}
impl ToGlEnum for MinifySamplerFilter {
#[inline]
fn to_glenum(&self) -> gl::types::GLenum {
match *self {
MinifySamplerFilter::Nearest => gl::NEAREST,
MinifySamplerFilter::Linear => gl::LINEAR,
MinifySamplerFilter::NearestMipmapNearest => gl::NEAREST_MIPMAP_NEAREST,
MinifySamplerFilter::LinearMipmapNearest => gl::LINEAR_MIPMAP_NEAREST,
MinifySamplerFilter::NearestMipmapLinear => gl::NEAREST_MIPMAP_LINEAR,
MinifySamplerFilter::LinearMipmapLinear => gl::LINEAR_MIPMAP_LINEAR,
}
}
}
/// The depth texture comparison operation to use when comparing the r value to the value in the
/// currently bound texture.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum DepthTextureComparison {
/// The r value is less than or equal to the texture value
LessOrEqual,
/// The r value is greater than or equal to the texture value
GreaterOrEqual,
/// The r value is less than to the texture value
Less,
/// The r value is greater than to the texture value
Greater,
/// The r value is equal to the texture value
Equal,
/// The r value is not equal to the texture value
NotEqual,
/// Always return 1.0 (true)
Always,
/// Never return 1.0 will return 0.0 (false)
Never,
}
impl ToGlEnum for DepthTextureComparison {
#[inline]
fn to_glenum(&self) -> gl::types::GLenum {
match *self {
DepthTextureComparison::LessOrEqual => gl::LEQUAL,
DepthTextureComparison::GreaterOrEqual => gl::GEQUAL,
DepthTextureComparison::Less => gl::LESS,
DepthTextureComparison::Greater => gl::GREATER,
DepthTextureComparison::Equal => gl::EQUAL,
DepthTextureComparison::NotEqual => gl::NOTEQUAL,
DepthTextureComparison::Always => gl::ALWAYS,
DepthTextureComparison::Never => gl::NEVER,
}
}
}
/// A sampler.
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct Sampler<'t, T>(pub &'t T, pub SamplerBehavior);
impl<'t, T: 't> Sampler<'t, T> {
/// Builds a new `Sampler` with default parameters.
pub fn new(texture: &'t T) -> Sampler<'t, T> {
Sampler(texture, Default::default())
}
/// Changes the wrap functions of all three coordinates.
pub fn wrap_function(mut self, function: SamplerWrapFunction) -> Sampler<'t, T> {
self.1.wrap_function = (function, function, function);
self
}
/// Changes the minifying filter of the sampler.
pub fn minify_filter(mut self, filter: MinifySamplerFilter) -> Sampler<'t, T> {
self.1.minify_filter = filter;
self
}
/// Changes the magnifying filter of the sampler.
pub fn magnify_filter(mut self, filter: MagnifySamplerFilter) -> Sampler<'t, T> {
self.1.magnify_filter = filter;
self
}
/// Sets the depth texture comparison method.
pub fn depth_texture_comparison(mut self, comparison: Option<DepthTextureComparison>) -> Sampler<'t, T> {
self.1.depth_texture_comparison = comparison;
self
}
/// Changes the magnifying filter of the sampler.
pub fn anisotropy(mut self, level: u16) -> Sampler<'t, T> {
self.1.max_anisotropy = level;
self
}
}
impl<'t, T: 't> Copy for Sampler<'t, T> {}
impl<'t, T: 't> Clone for Sampler<'t, T> {
fn clone(&self) -> Self {
*self
}
}
/// Behavior of a sampler.
// TODO: GL_TEXTURE_BORDER_COLOR, GL_TEXTURE_MIN_LOD, GL_TEXTURE_MAX_LOD, GL_TEXTURE_LOD_BIAS
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct SamplerBehavior {
/// Functions to use for the X, Y, and Z coordinates.
pub wrap_function: (SamplerWrapFunction, SamplerWrapFunction, SamplerWrapFunction),
/// Filter to use when minifying the texture.
pub minify_filter: MinifySamplerFilter,
/// Filter to use when magnifying the texture.
pub magnify_filter: MagnifySamplerFilter,
/// The depth texture comparison function to use. Default value is None.
pub depth_texture_comparison: Option<DepthTextureComparison>,
/// `1` means no anisotropic filtering, any value above `1` sets the max anisotropy.
///
/// ## Compatibility
///
/// This parameter is always available. However it is ignored on hardware that does
/// not support anisotropic filtering.
///
/// If you set the value to a value higher than what the hardware supports, it will
/// be clamped.
pub max_anisotropy: u16,
}
impl Default for SamplerBehavior {
#[inline]
fn default() -> SamplerBehavior {
SamplerBehavior {
wrap_function: (
SamplerWrapFunction::Mirror,
SamplerWrapFunction::Mirror,
SamplerWrapFunction::Mirror
),
minify_filter: MinifySamplerFilter::LinearMipmapLinear,
magnify_filter: MagnifySamplerFilter::Linear,
depth_texture_comparison: None,
max_anisotropy: 1,
}
}
}