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/strings/format.h>
17 Sampler::Sampler(const Texture &tex):
21 if(this!=&tex.get_default_sampler())
22 throw invalid_argument("Sampler::Sampler");
29 min_filter = NEAREST_MIPMAP_LINEAR;
31 max_anisotropy = 1.0f;
40 void Sampler::update_parameter(int mask) const
42 if(!owner->get_id() || (!ARB_direct_state_access && TexUnit::current().get_texture()!=owner))
44 TexUnit *unit = TexUnit::find_unit(owner);
55 set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter);
57 set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter);
58 if(mask&MAX_ANISOTROPY)
59 set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
61 set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s);
63 set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t);
65 set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r);
68 set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
70 set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func);
74 void Sampler::set_parameter_i(unsigned param, int value) const
76 if(ARB_direct_state_access)
77 glTextureParameteri(owner->get_id(), param, value);
79 glTexParameteri(owner->get_target(), param, value);
82 void Sampler::set_parameter_f(unsigned param, float value) const
84 if(ARB_direct_state_access)
85 glTextureParameterf(owner->get_id(), param, value);
87 glTexParameterf(owner->get_target(), param, value);
90 void Sampler::set_min_filter(TextureFilter f)
93 update_parameter(MIN_FILTER);
96 void Sampler::set_mag_filter(TextureFilter f)
99 update_parameter(MAG_FILTER);
102 void Sampler::set_filter(TextureFilter f)
105 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
108 void Sampler::set_max_anisotropy(float a)
111 throw invalid_argument("Sampler::set_max_anisotropy");
113 static Require _req(EXT_texture_filter_anisotropic);
115 if(EXT_texture_filter_anisotropic)
116 update_parameter(MAX_ANISOTROPY);
119 void Sampler::set_wrap(TextureWrap w)
127 void Sampler::set_wrap_s(TextureWrap w)
130 update_parameter(WRAP_S);
133 void Sampler::set_wrap_t(TextureWrap w)
136 update_parameter(WRAP_T);
139 void Sampler::set_wrap_r(TextureWrap w)
141 static Require _req(EXT_texture3D);
143 update_parameter(WRAP_R);
146 void Sampler::disable_compare()
149 update_parameter(COMPARE);
152 void Sampler::set_compare(Predicate f)
154 static Require _req(ARB_shadow);
157 update_parameter(COMPARE);
160 void Sampler::bind_to(unsigned i) const
162 TexUnit &unit = TexUnit::get_unit(i);
163 if(owner!=unit.get_texture())
164 throw invalid_operation("Sampler::bind_to");
168 update_parameter(dirty_params);
173 void Sampler::unload()
175 // TODO check which params actually need refreshing
181 Sampler::Loader::Loader(Sampler &s):
182 DataFile::ObjectLoader<Sampler>(s)
184 add("filter", &Loader::filter);
185 add("mag_filter", &Loader::mag_filter);
186 add("max_anisotropy", &Loader::max_anisotropy);
187 add("min_filter", &Loader::min_filter);
188 add("wrap", &Loader::wrap);
189 add("wrap_r", &Loader::wrap_r);
190 add("wrap_s", &Loader::wrap_s);
191 add("wrap_t", &Loader::wrap_t);
194 void Sampler::Loader::filter(TextureFilter f)
199 void Sampler::Loader::mag_filter(TextureFilter f)
201 obj.set_mag_filter(f);
204 void Sampler::Loader::max_anisotropy(float a)
206 obj.set_max_anisotropy(a);
209 void Sampler::Loader::min_filter(TextureFilter f)
211 obj.set_min_filter(f);
214 void Sampler::Loader::wrap(TextureWrap w)
219 void Sampler::Loader::wrap_r(TextureWrap w)
224 void Sampler::Loader::wrap_s(TextureWrap w)
229 void Sampler::Loader::wrap_t(TextureWrap w)
235 bool is_mipmapped(TextureFilter filter)
237 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
238 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
241 void operator>>(const LexicalConverter &c, TextureFilter &tf)
243 if(c.get()=="NEAREST")
245 else if(c.get()=="LINEAR")
247 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
248 tf = NEAREST_MIPMAP_NEAREST;
249 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
250 tf = NEAREST_MIPMAP_LINEAR;
251 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
252 tf = LINEAR_MIPMAP_NEAREST;
253 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
254 tf = LINEAR_MIPMAP_LINEAR;
256 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
259 void operator>>(const LexicalConverter &c, TextureWrap &tw)
261 if(c.get()=="REPEAT")
263 else if(c.get()=="CLAMP_TO_EDGE")
265 else if(c.get()=="MIRRORED_REPEAT")
266 tw = MIRRORED_REPEAT;
268 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));