X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Fsampler.cpp;h=bb37b0747e7d12c732ac4e57234c6b8bc3836344;hp=93b3b5c9f702e69e091ffbd480c438d7fe8f695c;hb=dbc91b65728ab9c0e574bb1127cfe4d2da55de7f;hpb=f1244e29afd2a36aafc2373d485457b4cb0411ff diff --git a/source/core/sampler.cpp b/source/core/sampler.cpp index 93b3b5c9..bb37b074 100644 --- a/source/core/sampler.cpp +++ b/source/core/sampler.cpp @@ -16,10 +16,17 @@ namespace Msp { namespace GL { Sampler::Sampler(): - owner(0) + min_filter(NEAREST_MIPMAP_LINEAR), + mag_filter(LINEAR), + max_anisotropy(1.0f), + wrap_s(REPEAT), + wrap_t(REPEAT), + wrap_r(REPEAT), + border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)), + compare(false), + cmp_func(LEQUAL), + dirty_params(0) { - init(); - Require _req(ARB_sampler_objects); if(ARB_direct_state_access) glCreateSamplers(1, &id); @@ -27,105 +34,30 @@ Sampler::Sampler(): glGenSamplers(1, &id); } -Sampler::Sampler(const Texture &tex): - id(0), - owner(&tex) -{ - if(this!=&tex.get_default_sampler()) - throw invalid_argument("Sampler::Sampler"); - - init(); -} - -void Sampler::init() -{ - min_filter = NEAREST_MIPMAP_LINEAR; - mag_filter = LINEAR; - max_anisotropy = 1.0f; - wrap_s = REPEAT; - wrap_t = REPEAT; - wrap_r = REPEAT; - border_color = Color(0.0f, 0.0f, 0.0f, 0.0f); - compare = false; - cmp_func = LEQUAL; - dirty_params = 0; -} - void Sampler::update_parameter(int mask) const { - if(owner) - { - if(!owner->get_id()) - { - dirty_params |= mask; - return; - } - - if(!ARB_direct_state_access && TexUnit::current().get_texture()!=owner) - { - TexUnit *unit = TexUnit::find_unit(owner); - if(!unit) - { - dirty_params |= mask; - return; - } - - unit->bind(); - } - } - if(mask&MIN_FILTER) - set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter); + glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, min_filter); if(mask&MAG_FILTER) - set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter); + glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, mag_filter); if(mask&MAX_ANISOTROPY) - set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy); + glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy); if(mask&WRAP_S) - set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s); + glSamplerParameteri(id, GL_TEXTURE_WRAP_S, wrap_s); if(mask&WRAP_T) - set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t); + glSamplerParameteri(id, GL_TEXTURE_WRAP_T, wrap_t); if(mask&WRAP_R) - set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r); + glSamplerParameteri(id, GL_TEXTURE_WRAP_R, wrap_r); if(mask&BORDER_COLOR) - set_parameter_fv(GL_TEXTURE_BORDER_COLOR, &border_color.r); + glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &border_color.r); if(mask&COMPARE) { - set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE)); + glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE)); if(compare) - set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func); + glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, cmp_func); } } -void Sampler::set_parameter_i(unsigned param, int value) const -{ - if(id) - glSamplerParameteri(id, param, value); - else if(ARB_direct_state_access) - glTextureParameteri(owner->get_id(), param, value); - else - glTexParameteri(owner->get_target(), param, value); -} - -void Sampler::set_parameter_f(unsigned param, float value) const -{ - if(id) - glSamplerParameterf(id, param, value); - else if(ARB_direct_state_access) - glTextureParameterf(owner->get_id(), param, value); - else - glTexParameterf(owner->get_target(), param, value); -} - -void Sampler::set_parameter_fv(unsigned param, const float *value) const -{ - if(id) - glSamplerParameterfv(id, param, value); - else if(ARB_direct_state_access) - glTextureParameterfv(owner->get_id(), param, value); - else - glTexParameterfv(owner->get_target(), param, value); -} - void Sampler::set_min_filter(TextureFilter f) { min_filter = f; @@ -199,20 +131,16 @@ void Sampler::set_compare(Predicate f) static Require _req(ARB_shadow); compare = true; cmp_func = f; - update_parameter(COMPARE); + dirty_params |= COMPARE; } void Sampler::bind_to(unsigned i) const { TexUnit &unit = TexUnit::get_unit(i); - if(owner && owner!=unit.get_texture()) - throw invalid_operation("Sampler::bind_to"); - const Sampler *cur = unit.get_sampler(); if(unit.set_sampler(this)) { - if(!owner || (cur && cur->id)) - glBindSampler(i, id); + glBindSampler(i, id); if(dirty_params) { @@ -230,32 +158,10 @@ const Sampler *Sampler::current(unsigned i) void Sampler::unbind_from(unsigned i) { TexUnit &unit = TexUnit::get_unit(i); - const Sampler *cur = unit.get_sampler(); - if(unit.set_sampler(0) && cur->id) + if(unit.set_sampler(0)) glBindSampler(i, 0); } -void Sampler::unload() -{ - if(owner && !owner->get_id()) - { - if(min_filter!=NEAREST_MIPMAP_LINEAR) - dirty_params |= MIN_FILTER; - if(mag_filter!=LINEAR) - dirty_params |= MAG_FILTER; - if(max_anisotropy!=1.0f) - dirty_params |= MAX_ANISOTROPY; - if(wrap_s!=REPEAT) - dirty_params |= WRAP_S; - if(wrap_t!=REPEAT) - dirty_params |= WRAP_T; - if(wrap_r!=REPEAT) - dirty_params |= WRAP_R; - if(compare || cmp_func!=LEQUAL) - dirty_params |= COMPARE; - } -} - void Sampler::set_debug_name(const string &name) { #ifdef DEBUG