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>
9 #include "sampler_backend.h"
16 OpenGLSampler::OpenGLSampler()
18 static Require _req(ARB_sampler_objects);
19 static Require _req2(EXT_texture3D);
20 static Require _req3(ARB_shadow);
22 if(ARB_direct_state_access)
23 glCreateSamplers(1, &id);
25 glGenSamplers(1, &id);
28 OpenGLSampler::OpenGLSampler(OpenGLSampler &&other):
34 OpenGLSampler::~OpenGLSampler()
37 glDeleteSamplers(1, &id);
40 bool OpenGLSampler::check_anisotropic(bool require)
43 static Require _req(EXT_texture_filter_anisotropic);
44 return EXT_texture_filter_anisotropic;
47 void OpenGLSampler::update(unsigned mask) const
49 const Sampler &self = *static_cast<const Sampler *>(this);
50 if(mask&Sampler::MIN_FILTER)
51 glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, get_gl_filter(self.min_filter));
52 if(mask&Sampler::MAG_FILTER)
53 glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, get_gl_filter(self.mag_filter));
54 if(mask&Sampler::MAX_ANISOTROPY)
55 glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, self.max_anisotropy);
56 if(mask&Sampler::WRAP_S)
57 glSamplerParameteri(id, GL_TEXTURE_WRAP_S, get_gl_wrap(self.wrap_s));
58 if(mask&Sampler::WRAP_T)
59 glSamplerParameteri(id, GL_TEXTURE_WRAP_T, get_gl_wrap(self.wrap_t));
60 if(mask&Sampler::WRAP_R)
61 glSamplerParameteri(id, GL_TEXTURE_WRAP_R, get_gl_wrap(self.wrap_r));
62 if(mask&Sampler::BORDER_COLOR)
63 glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &self.border_color.r);
64 if(mask&Sampler::COMPARE)
66 glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (self.compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
68 glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, get_gl_predicate(self.cmp_func));
72 void OpenGLSampler::set_debug_name(const string &name)
76 glObjectLabel(GL_SAMPLER, id, name.size(), name.c_str());
83 unsigned get_gl_filter(unsigned filter)
87 case NEAREST: return GL_NEAREST;
88 case LINEAR: return GL_LINEAR;
89 case NEAREST_MIPMAP_NEAREST: return GL_NEAREST_MIPMAP_NEAREST;
90 case NEAREST_MIPMAP_LINEAR: return GL_NEAREST_MIPMAP_LINEAR;
91 case LINEAR_MIPMAP_NEAREST: return GL_LINEAR_MIPMAP_NEAREST;
92 case LINEAR_MIPMAP_LINEAR: return GL_LINEAR_MIPMAP_LINEAR;
93 default: throw invalid_argument("get_gl_filter");
97 unsigned get_gl_wrap(unsigned wrap)
101 case REPEAT: return GL_REPEAT;
102 case CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE;
103 case CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER;
104 case MIRRORED_REPEAT: return GL_MIRRORED_REPEAT;
105 default: throw invalid_argument("get_gl_wrap");