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>
17 min_filter(NEAREST_MIPMAP_LINEAR),
23 border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)),
28 Require _req(ARB_sampler_objects);
29 Require _req2(EXT_texture3D);
30 Require _req3(ARB_shadow);
31 if(ARB_direct_state_access)
32 glCreateSamplers(1, &id);
34 glGenSamplers(1, &id);
37 void Sampler::update() const
39 if(dirty_params&MIN_FILTER)
40 glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, get_gl_filter(min_filter));
41 if(dirty_params&MAG_FILTER)
42 glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, get_gl_filter(mag_filter));
43 if(dirty_params&MAX_ANISOTROPY)
44 glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
45 if(dirty_params&WRAP_S)
46 glSamplerParameteri(id, GL_TEXTURE_WRAP_S, get_gl_wrap(wrap_s));
47 if(dirty_params&WRAP_T)
48 glSamplerParameteri(id, GL_TEXTURE_WRAP_T, get_gl_wrap(wrap_t));
49 if(dirty_params&WRAP_R)
50 glSamplerParameteri(id, GL_TEXTURE_WRAP_R, get_gl_wrap(wrap_r));
51 if(dirty_params&BORDER_COLOR)
52 glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &border_color.r);
53 if(dirty_params&COMPARE)
55 glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
57 glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, get_gl_predicate(cmp_func));
63 void Sampler::set_min_filter(TextureFilter f)
66 dirty_params |= MIN_FILTER;
69 void Sampler::set_mag_filter(TextureFilter f)
72 dirty_params |= MAG_FILTER;
75 void Sampler::set_filter(TextureFilter f)
78 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
81 void Sampler::set_max_anisotropy(float a)
84 throw invalid_argument("Sampler::set_max_anisotropy");
86 static Require _req(EXT_texture_filter_anisotropic);
88 if(EXT_texture_filter_anisotropic)
89 dirty_params |= MAX_ANISOTROPY;
92 void Sampler::set_wrap(TextureWrap w)
99 void Sampler::set_wrap_s(TextureWrap w)
102 dirty_params |= WRAP_S;
105 void Sampler::set_wrap_t(TextureWrap w)
108 dirty_params |= WRAP_T;
111 void Sampler::set_wrap_r(TextureWrap w)
114 dirty_params |= WRAP_R;
117 void Sampler::set_border_color(const Color &c)
120 dirty_params |= BORDER_COLOR;
123 void Sampler::disable_compare()
126 dirty_params |= COMPARE;
129 void Sampler::set_compare(Predicate f)
133 dirty_params |= COMPARE;
136 void Sampler::set_debug_name(const string &name)
140 glObjectLabel(GL_SAMPLER, id, name.size(), name.c_str());
147 Sampler::Loader::Loader(Sampler &s):
148 DataFile::ObjectLoader<Sampler>(s)
150 add("border_color", &Loader::border_color);
151 add("compare", &Loader::compare);
152 add("filter", &Loader::filter);
153 add("mag_filter", &Loader::mag_filter);
154 add("max_anisotropy", &Loader::max_anisotropy);
155 add("min_filter", &Loader::min_filter);
156 add("wrap", &Loader::wrap);
157 add("wrap_r", &Loader::wrap_r);
158 add("wrap_s", &Loader::wrap_s);
159 add("wrap_t", &Loader::wrap_t);
162 void Sampler::Loader::border_color(float r, float g, float b, float a)
164 obj.set_border_color(Color(r, g, b, a));
167 void Sampler::Loader::compare(Predicate c)
172 void Sampler::Loader::filter(TextureFilter f)
177 void Sampler::Loader::mag_filter(TextureFilter f)
179 obj.set_mag_filter(f);
182 void Sampler::Loader::max_anisotropy(float a)
184 obj.set_max_anisotropy(a);
187 void Sampler::Loader::min_filter(TextureFilter f)
189 obj.set_min_filter(f);
192 void Sampler::Loader::wrap(TextureWrap w)
197 void Sampler::Loader::wrap_r(TextureWrap w)
202 void Sampler::Loader::wrap_s(TextureWrap w)
207 void Sampler::Loader::wrap_t(TextureWrap w)
213 bool is_mipmapped(TextureFilter filter)
215 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
216 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
219 unsigned get_gl_filter(TextureFilter filter)
223 case NEAREST: return GL_NEAREST;
224 case LINEAR: return GL_LINEAR;
225 case NEAREST_MIPMAP_NEAREST: return GL_NEAREST_MIPMAP_NEAREST;
226 case NEAREST_MIPMAP_LINEAR: return GL_NEAREST_MIPMAP_LINEAR;
227 case LINEAR_MIPMAP_NEAREST: return GL_LINEAR_MIPMAP_NEAREST;
228 case LINEAR_MIPMAP_LINEAR: return GL_LINEAR_MIPMAP_LINEAR;
229 default: throw invalid_argument("get_gl_filter");
233 unsigned get_gl_wrap(TextureWrap wrap)
237 case REPEAT: return GL_REPEAT;
238 case CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE;
239 case CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER;
240 case MIRRORED_REPEAT: return GL_MIRRORED_REPEAT;
241 default: throw invalid_argument("get_gl_wrap");
245 void operator>>(const LexicalConverter &c, TextureFilter &tf)
247 if(c.get()=="NEAREST")
249 else if(c.get()=="LINEAR")
251 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
252 tf = NEAREST_MIPMAP_NEAREST;
253 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
254 tf = NEAREST_MIPMAP_LINEAR;
255 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
256 tf = LINEAR_MIPMAP_NEAREST;
257 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
258 tf = LINEAR_MIPMAP_LINEAR;
260 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
263 void operator>>(const LexicalConverter &c, TextureWrap &tw)
265 if(c.get()=="REPEAT")
267 else if(c.get()=="CLAMP_TO_EDGE")
269 else if(c.get()=="CLAMP_TO_BORDER")
270 tw = CLAMP_TO_BORDER;
271 else if(c.get()=="MIRRORED_REPEAT")
272 tw = MIRRORED_REPEAT;
274 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));