1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_sampler_objects.h>
3 #include <msp/gl/extensions/arb_shadow.h>
4 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
5 #include <msp/gl/extensions/ext_texture3d.h>
6 #include <msp/gl/extensions/khr_debug.h>
7 #include <msp/strings/format.h>
19 min_filter(NEAREST_MIPMAP_LINEAR),
25 border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)),
30 Require _req(ARB_sampler_objects);
31 if(ARB_direct_state_access)
32 glCreateSamplers(1, &id);
34 glGenSamplers(1, &id);
37 void Sampler::update_parameter(int mask) const
40 glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, min_filter);
42 glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, mag_filter);
43 if(mask&MAX_ANISOTROPY)
44 glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
46 glSamplerParameteri(id, GL_TEXTURE_WRAP_S, wrap_s);
48 glSamplerParameteri(id, GL_TEXTURE_WRAP_T, wrap_t);
50 glSamplerParameteri(id, GL_TEXTURE_WRAP_R, wrap_r);
52 glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &border_color.r);
55 glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
57 glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, cmp_func);
61 void Sampler::set_min_filter(TextureFilter f)
64 update_parameter(MIN_FILTER);
67 void Sampler::set_mag_filter(TextureFilter f)
70 update_parameter(MAG_FILTER);
73 void Sampler::set_filter(TextureFilter f)
76 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
79 void Sampler::set_max_anisotropy(float a)
82 throw invalid_argument("Sampler::set_max_anisotropy");
84 static Require _req(EXT_texture_filter_anisotropic);
86 if(EXT_texture_filter_anisotropic)
87 update_parameter(MAX_ANISOTROPY);
90 void Sampler::set_wrap(TextureWrap w)
98 void Sampler::set_wrap_s(TextureWrap w)
101 update_parameter(WRAP_S);
104 void Sampler::set_wrap_t(TextureWrap w)
107 update_parameter(WRAP_T);
110 void Sampler::set_wrap_r(TextureWrap w)
112 static Require _req(EXT_texture3D);
114 update_parameter(WRAP_R);
117 void Sampler::set_border_color(const Color &c)
120 update_parameter(BORDER_COLOR);
123 void Sampler::disable_compare()
126 update_parameter(COMPARE);
129 void Sampler::set_compare(Predicate f)
131 static Require _req(ARB_shadow);
134 dirty_params |= COMPARE;
137 void Sampler::bind_to(unsigned i) const
139 TexUnit &unit = TexUnit::get_unit(i);
141 if(unit.set_sampler(this))
143 glBindSampler(i, id);
147 update_parameter(dirty_params);
153 const Sampler *Sampler::current(unsigned i)
155 return TexUnit::get_unit(i).get_sampler();
158 void Sampler::unbind_from(unsigned i)
160 TexUnit &unit = TexUnit::get_unit(i);
161 if(unit.set_sampler(0))
165 void Sampler::set_debug_name(const string &name)
169 glObjectLabel(GL_SAMPLER, id, name.size(), name.c_str());
176 Sampler::Loader::Loader(Sampler &s):
177 DataFile::ObjectLoader<Sampler>(s)
179 add("border_color", &Loader::border_color);
180 add("compare", &Loader::compare);
181 add("filter", &Loader::filter);
182 add("mag_filter", &Loader::mag_filter);
183 add("max_anisotropy", &Loader::max_anisotropy);
184 add("min_filter", &Loader::min_filter);
185 add("wrap", &Loader::wrap);
186 add("wrap_r", &Loader::wrap_r);
187 add("wrap_s", &Loader::wrap_s);
188 add("wrap_t", &Loader::wrap_t);
191 void Sampler::Loader::border_color(float r, float g, float b, float a)
193 obj.set_border_color(Color(r, g, b, a));
196 void Sampler::Loader::compare(Predicate c)
201 void Sampler::Loader::filter(TextureFilter f)
206 void Sampler::Loader::mag_filter(TextureFilter f)
208 obj.set_mag_filter(f);
211 void Sampler::Loader::max_anisotropy(float a)
213 obj.set_max_anisotropy(a);
216 void Sampler::Loader::min_filter(TextureFilter f)
218 obj.set_min_filter(f);
221 void Sampler::Loader::wrap(TextureWrap w)
226 void Sampler::Loader::wrap_r(TextureWrap w)
231 void Sampler::Loader::wrap_s(TextureWrap w)
236 void Sampler::Loader::wrap_t(TextureWrap w)
242 bool is_mipmapped(TextureFilter filter)
244 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
245 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
248 void operator>>(const LexicalConverter &c, TextureFilter &tf)
250 if(c.get()=="NEAREST")
252 else if(c.get()=="LINEAR")
254 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
255 tf = NEAREST_MIPMAP_NEAREST;
256 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
257 tf = NEAREST_MIPMAP_LINEAR;
258 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
259 tf = LINEAR_MIPMAP_NEAREST;
260 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
261 tf = LINEAR_MIPMAP_LINEAR;
263 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
266 void operator>>(const LexicalConverter &c, TextureWrap &tw)
268 if(c.get()=="REPEAT")
270 else if(c.get()=="CLAMP_TO_EDGE")
272 else if(c.get()=="CLAMP_TO_BORDER")
273 tw = CLAMP_TO_BORDER;
274 else if(c.get()=="MIRRORED_REPEAT")
275 tw = MIRRORED_REPEAT;
277 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));