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>
18 min_filter(NEAREST_MIPMAP_LINEAR),
24 border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)),
29 Require _req(ARB_sampler_objects);
30 if(ARB_direct_state_access)
31 glCreateSamplers(1, &id);
33 glGenSamplers(1, &id);
36 void Sampler::update() const
38 if(dirty_params&MIN_FILTER)
39 glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, get_gl_filter(min_filter));
40 if(dirty_params&MAG_FILTER)
41 glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, get_gl_filter(mag_filter));
42 if(dirty_params&MAX_ANISOTROPY)
43 glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
44 if(dirty_params&WRAP_S)
45 glSamplerParameteri(id, GL_TEXTURE_WRAP_S, get_gl_wrap(wrap_s));
46 if(dirty_params&WRAP_T)
47 glSamplerParameteri(id, GL_TEXTURE_WRAP_T, get_gl_wrap(wrap_t));
48 if(dirty_params&WRAP_R)
49 glSamplerParameteri(id, GL_TEXTURE_WRAP_R, get_gl_wrap(wrap_r));
50 if(dirty_params&BORDER_COLOR)
51 glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &border_color.r);
52 if(dirty_params&COMPARE)
54 glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
56 glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, get_gl_predicate(cmp_func));
62 void Sampler::set_min_filter(TextureFilter f)
65 dirty_params |= MIN_FILTER;
68 void Sampler::set_mag_filter(TextureFilter f)
71 dirty_params |= MAG_FILTER;
74 void Sampler::set_filter(TextureFilter f)
77 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
80 void Sampler::set_max_anisotropy(float a)
83 throw invalid_argument("Sampler::set_max_anisotropy");
85 static Require _req(EXT_texture_filter_anisotropic);
87 if(EXT_texture_filter_anisotropic)
88 dirty_params |= MAX_ANISOTROPY;
91 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)
113 static Require _req(EXT_texture3D);
115 dirty_params |= WRAP_R;
118 void Sampler::set_border_color(const Color &c)
121 dirty_params |= BORDER_COLOR;
124 void Sampler::disable_compare()
127 dirty_params |= COMPARE;
130 void Sampler::set_compare(Predicate f)
132 static Require _req(ARB_shadow);
135 dirty_params |= COMPARE;
138 void Sampler::set_debug_name(const string &name)
142 glObjectLabel(GL_SAMPLER, id, name.size(), name.c_str());
149 Sampler::Loader::Loader(Sampler &s):
150 DataFile::ObjectLoader<Sampler>(s)
152 add("border_color", &Loader::border_color);
153 add("compare", &Loader::compare);
154 add("filter", &Loader::filter);
155 add("mag_filter", &Loader::mag_filter);
156 add("max_anisotropy", &Loader::max_anisotropy);
157 add("min_filter", &Loader::min_filter);
158 add("wrap", &Loader::wrap);
159 add("wrap_r", &Loader::wrap_r);
160 add("wrap_s", &Loader::wrap_s);
161 add("wrap_t", &Loader::wrap_t);
164 void Sampler::Loader::border_color(float r, float g, float b, float a)
166 obj.set_border_color(Color(r, g, b, a));
169 void Sampler::Loader::compare(Predicate c)
174 void Sampler::Loader::filter(TextureFilter f)
179 void Sampler::Loader::mag_filter(TextureFilter f)
181 obj.set_mag_filter(f);
184 void Sampler::Loader::max_anisotropy(float a)
186 obj.set_max_anisotropy(a);
189 void Sampler::Loader::min_filter(TextureFilter f)
191 obj.set_min_filter(f);
194 void Sampler::Loader::wrap(TextureWrap w)
199 void Sampler::Loader::wrap_r(TextureWrap w)
204 void Sampler::Loader::wrap_s(TextureWrap w)
209 void Sampler::Loader::wrap_t(TextureWrap w)
215 bool is_mipmapped(TextureFilter filter)
217 return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
218 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
221 GLenum get_gl_filter(TextureFilter filter)
225 case NEAREST: return GL_NEAREST;
226 case LINEAR: return GL_LINEAR;
227 case NEAREST_MIPMAP_NEAREST: return GL_NEAREST_MIPMAP_NEAREST;
228 case NEAREST_MIPMAP_LINEAR: return GL_NEAREST_MIPMAP_LINEAR;
229 case LINEAR_MIPMAP_NEAREST: return GL_LINEAR_MIPMAP_NEAREST;
230 case LINEAR_MIPMAP_LINEAR: return GL_LINEAR_MIPMAP_LINEAR;
231 default: throw invalid_argument("get_gl_filter");
235 GLenum get_gl_wrap(TextureWrap wrap)
239 case REPEAT: return GL_REPEAT;
240 case CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE;
241 case CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER;
242 case MIRRORED_REPEAT: return GL_MIRRORED_REPEAT;
243 default: throw invalid_argument("get_gl_wrap");
247 void operator>>(const LexicalConverter &c, TextureFilter &tf)
249 if(c.get()=="NEAREST")
251 else if(c.get()=="LINEAR")
253 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
254 tf = NEAREST_MIPMAP_NEAREST;
255 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
256 tf = NEAREST_MIPMAP_LINEAR;
257 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
258 tf = LINEAR_MIPMAP_NEAREST;
259 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
260 tf = LINEAR_MIPMAP_LINEAR;
262 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
265 void operator>>(const LexicalConverter &c, TextureWrap &tw)
267 if(c.get()=="REPEAT")
269 else if(c.get()=="CLAMP_TO_EDGE")
271 else if(c.get()=="CLAMP_TO_BORDER")
272 tw = CLAMP_TO_BORDER;
273 else if(c.get()=="MIRRORED_REPEAT")
274 tw = MIRRORED_REPEAT;
276 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));