27 static constexpr GLenum mag_filter_lut[] =
33 static constexpr GLenum min_filter_lut[][2] =
36 GL_NEAREST_MIPMAP_NEAREST,
37 GL_NEAREST_MIPMAP_LINEAR
40 GL_LINEAR_MIPMAP_NEAREST,
41 GL_LINEAR_MIPMAP_LINEAR
45 static constexpr GLenum wrap_lut[] =
51 GL_MIRROR_CLAMP_TO_EDGE
54 static constexpr GLenum compare_func_lut[] =
81 const std::array<float, 4>& border_color
84 glCreateSamplers(1, &m_gl_named_sampler);
103 glDeleteSamplers(1, &m_gl_named_sampler);
108 if (m_mag_filter != filter)
110 m_mag_filter = filter;
111 const auto gl_mag_filter = mag_filter_lut[std::to_underlying(m_mag_filter)];
112 glSamplerParameteri(m_gl_named_sampler, GL_TEXTURE_MAG_FILTER, gl_mag_filter);
118 if (m_min_filter != filter)
120 m_min_filter = filter;
121 const auto gl_min_filter = min_filter_lut[std::to_underlying(m_min_filter)][std::to_underlying(m_mipmap_mode)];
122 glSamplerParameteri(m_gl_named_sampler, GL_TEXTURE_MIN_FILTER, gl_min_filter);
128 if (m_mipmap_mode != mode)
130 m_mipmap_mode = mode;
131 const auto gl_min_filter = min_filter_lut[std::to_underlying(m_min_filter)][std::to_underlying(m_mipmap_mode)];
132 glSamplerParameteri(m_gl_named_sampler, GL_TEXTURE_MIN_FILTER, gl_min_filter);
138 if (m_address_mode_u != mode)
140 m_address_mode_u = mode;
141 const auto gl_wrap_s = wrap_lut[std::to_underlying(m_address_mode_u)];
142 glSamplerParameteri(m_gl_named_sampler, GL_TEXTURE_WRAP_S, gl_wrap_s);
148 if (m_address_mode_v != mode)
150 m_address_mode_v = mode;
151 const auto gl_wrap_t = wrap_lut[std::to_underlying(m_address_mode_v)];
152 glSamplerParameteri(m_gl_named_sampler, GL_TEXTURE_WRAP_T, gl_wrap_t);
158 if (m_address_mode_w != mode)
160 m_address_mode_w = mode;
161 const auto gl_wrap_r = wrap_lut[std::to_underlying(m_address_mode_w)];
162 glSamplerParameteri(m_gl_named_sampler, GL_TEXTURE_WRAP_R, gl_wrap_r);
168 if (m_mip_lod_bias != bias)
170 m_mip_lod_bias = bias;
171 glSamplerParameterf(m_gl_named_sampler, GL_TEXTURE_LOD_BIAS, m_mip_lod_bias);
177 if (m_max_anisotropy != anisotropy)
179 m_max_anisotropy = anisotropy;
180 glSamplerParameterf(m_gl_named_sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, m_max_anisotropy);
186 if (m_compare_enabled != enabled)
188 m_compare_enabled = enabled;
189 glSamplerParameteri(m_gl_named_sampler, GL_TEXTURE_COMPARE_MODE, (m_compare_enabled) ? GL_COMPARE_REF_TO_TEXTURE : GL_NONE);
195 if (m_compare_op != op)
198 const auto gl_compare_func = compare_func_lut[std::to_underlying(m_compare_op)];
199 glSamplerParameteri(m_gl_named_sampler, GL_TEXTURE_COMPARE_FUNC, gl_compare_func);
205 if (m_min_lod != lod)
208 glSamplerParameterf(m_gl_named_sampler, GL_TEXTURE_MIN_LOD, m_min_lod);
214 if (m_max_lod != lod)
217 glSamplerParameterf(m_gl_named_sampler, GL_TEXTURE_MAX_LOD, m_max_lod);
223 if (m_border_color !=
color)
225 m_border_color =
color;
226 glSamplerParameterfv(m_gl_named_sampler, GL_TEXTURE_BORDER_COLOR, m_border_color.data());
void set_min_lod(float lod)
Sets the minimum clamp value of the computed LOD.
void set_max_anisotropy(float anisotropy)
Sets the anisotropy clamp value.
void set_address_mode_w(sampler_address_mode mode)
Sets the addressing mode for W-coordinates outside [0, 1).
void set_mipmap_mode(sampler_mipmap_mode mode)
Sets the mipmap filter to apply to lookups.
void set_compare_op(gl::compare_op op)
Sets the comparison operator to apply to fetched data, if compare is enabled.
void set_address_mode_v(sampler_address_mode mode)
Sets the addressing mode for V-coordinates outside [0, 1).
void set_min_filter(sampler_filter filter)
Sets the minification filter to apply to lookups.
void set_mip_lod_bias(float bias)
Sets the bias to be added to mipmap LOD calculation.
void set_compare_enabled(bool enabled)
Enables or disables a comparison against a reference value during lookups.
sampler(sampler_filter mag_filter=sampler_filter::linear, sampler_filter min_filter=sampler_filter::nearest, sampler_mipmap_mode mipmap_mode=sampler_mipmap_mode::linear, sampler_address_mode address_mode_u=sampler_address_mode::repeat, sampler_address_mode address_mode_v=sampler_address_mode::repeat, sampler_address_mode address_mode_w=sampler_address_mode::repeat, float mip_lod_bias=0.0f, float max_anisotropy=0.0f, bool compare_enabled=false, gl::compare_op compare_op=gl::compare_op::less, float min_lod=-1000.0f, float max_lod=1000.0f, const std::array< float, 4 > &border_color={0.0f, 0.0f, 0.0f, 0.0f})
Constructs a sampler object.
void set_max_lod(float lod)
Sets the maximum clamp value of the computed LOD.
void set_border_color(const std::array< float, 4 > &color)
Sets the border color used for texture lookups.
void set_mag_filter(sampler_filter filter)
Sets the magnification filter to apply to lookups.
~sampler()
Destroys a sampler object.
void set_address_mode_u(sampler_address_mode mode)
Sets the addressing mode for U-coordinates outside [0, 1).
Graphics library interface.
sampler_address_mode
Behaviors of sampling with texture coordinates outside an image.
sampler_mipmap_mode
Mipmap modes used for texture lookups.
sampler_filter
Filters used for texture lookups.
compare_op
Comparison operators.
@ color
Vertex color (vec4)